c++14

Linker error (undefined reference) with `static constexpr const char*` and perfect-forwarding [duplicate]

£可爱£侵袭症+ 提交于 2019-12-12 16:36:00
问题 This question already has answers here : Undefined reference to static constexpr char[] (6 answers) Closed 4 years ago . #include <iostream> using namespace std; template<typename T> void print(T&& mX) { std::cout << std::forward<T>(mX) << std::endl; } struct SomeStruct { static constexpr const char* someString{"hello!"}; SomeStruct() { print(someString); } }; int main() { SomeStruct s{}; return 0; } clang++ -std=c++1y ./code.cpp -o code.o /tmp/code-a049fe.o: In function `SomeStruct:

How can I make a class that type-erases objects until a function is called on them without specifying the list of possible functions up front?

半世苍凉 提交于 2019-12-12 16:03:32
问题 Background The title probably sounds confusing, so let me explain. First of all, here is a minimal version of my implementation, so you can follow along with the concepts more easily. If you've seen some of Sean Parent's talks, you'll know he came up with a way to abstract polymorphism, allowing code such as this: std::vector<Drawable> figures{Circle{}, Square{}}; for (auto &&figure : figures) {draw(figure);} Notice that there are no pointers or anything. Calling draw on a Drawable will call

Resolving explicitly the scope of a class member

我的梦境 提交于 2019-12-12 14:32:53
问题 Consider the following example: #include <iostream> struct foo { void fun() const { std::cout << "foo::fun()" << std::endl; } }; auto main() -> int { foo f; f.fun(); f.foo::fun(); return 0; } DEMO As shown in the above example, member function foo::fun() is evoked with two different ways. In the second call (i.e., f.foo::fun() ), the scope of member class foo::fun() is explicitly disambiguated/Resolved. Questions: What's the difference between the two calls (i.e., f.fun() and f.foo::fun() )?

Constructors : difference between defaulting and delegating a parameter

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 14:23:14
问题 Today, I stumbled upon these standard declarations of std::vector constructors : // until C++14 explicit vector( const Allocator& alloc = Allocator() ); // since C++14 vector() : vector( Allocator() ) {} explicit vector( const Allocator& alloc ); This change can be seen in most of standard containers. A slightly different exemple is std::set : // until C++14 explicit set( const Compare& comp = Compare(), const Allocator& alloc = Allocator() ); // since C++14 set() : set( Compare() ) {}

External linkage for name inside unnamed namespace

不问归期 提交于 2019-12-12 13:26:36
问题 According to the clause 3.5/4 of C++ Standard: An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage. Simultanously in paragraph 7.3.1.1 we have note 96): Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit. How to explicitly make external linkage for name inside unnamed

Why 'enable_if' cannot be used to disable this declaration here

瘦欲@ 提交于 2019-12-12 13:05:51
问题 #include<string> #include<type_traits> template<typename... Args> class C { public: void foo(Args&&... args) { } template<typename = std::enable_if_t<(0 < sizeof...(Args))>> void foo(const Args&... args) { } }; int main() { C<> c; c.foo(); return 0; } Above code works as expacted (by me :)) and calls void foo(Args&&... args) at run-time in msvc 2015 but same code fails to even compile in both gcc 7.3 and clang 6.0.0 with error: error: no type named 'type' in 'std::enable_if'; 'enable_if'

Specializing class with SFINAE

社会主义新天地 提交于 2019-12-12 12:28:26
问题 I want to write a template class which checks a trait with SFINAE. As classes can not be "overloaded" as I read in that post: template overloading and SFINAE working only with functions but not classes I wrote the following code: class AA { public: using TRAIT = int; }; class BB { public: using TRAIT = float; }; template < typename T, typename UNUSED = void> class X; template < typename T> class X<T, typename std::enable_if< std::is_same< int, typename T::TRAIT>::value, int >::type> { public:

Initializing std::array with Static Storage Duration with a Parameter Pack Expansion and an Additional Value

我是研究僧i 提交于 2019-12-12 12:23:38
问题 While asking another question recently, I stumbled upon some strange behavior of GCC when initializing a std::array with a parameter pack expansion followed by another element . I have already discussed this briefly with Jarod42 in the comments there but I believe it should better be asked as a new question. For example, consider the following code that is supposed to provide a utility make_array function that takes an arbitrary number of parameters and std::forward s them to the std::array

Creating a `std::chrono::time_point` from a calendar date known at compile time

你。 提交于 2019-12-12 12:09:56
问题 This answer shows how to parse a string to a std::chrono::time_point , as follows: std::tm tm = {}; std::stringstream ss("Jan 9 2014 12:35:34"); ss >> std::get_time(&tm, "%b %d %Y %H:%M:%S"); auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm)); If I want to create a std::chrono::time_point from a (Gregorian) calendar date whose year, month, and day-of-month are known at compile time, is there any simpler way than parsing it from a string as suggested above? 回答1: Yes, you can do

Floating point division by zero not constexpr

邮差的信 提交于 2019-12-12 11:51:56
问题 When compiling this: constexpr double x {123.0}; constexpr double y = x / 0.0; std::cout << x << " / 0 = " << y << "\n"; The compiler (gcc 4.9.2, -std=c++11 or c++14) fails, giving error: (1.23e+2 / 0.0)' is not a constant expression constexpr double y = x / 0.0; How is the result (Inf) relevant when deciding if y can be a constexpr or not? For reference, this seems to be the way to do it: static constexpr double z = std::numeric_limits<double>::quiet_NaN(); static constexpr double w = std: