From the C++11 Wiki page: If a constexpr function or constructor is called with arguments which aren't constant expressions, the call behaves as if the function were not constexpr, and the resulting value is not a constant expression. Likewise, if the expression in the return statement of a constexpr function...
c++,templates,c++11,constexpr,argument-dependent-lookup
Just don't use constexpr: std::true_type has_some_property(Foo&& ); Does Foo have it? using has_it = decltype(has_some_property(std::declval<Foo>())); static_assert(has_it::value, "It should!"); This is an unevaluated context, so we never have to call any Foo constructor. And can sidestep the issues with constexpr of requiring constant expressions....
As commented by @Nawaz, the optimization is pointless. There will not be any runtime benefit and compiler is smart enough to optimize the conditions during compile time so there should not be much overhead during compile time. In any case, what you are envisaging is possible, provided you agree to...
Since this is not constexpr the access to this->t_ptr is not either. clang's error is a bit more helpful implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function Referring to: N3690 5.19/2 (emphasis added) A conditional-expression e is a core...
No, there is no such way. Sorry. N3583 is a paper proposing changes to allow what you are asking for....
c++,c++11,static-members,constexpr
As @dyp mentioned the solution for one compiles because function definitions are compiled after the class body. So it is like one has been declared like this class MyEnum { public: static constexpr const MyEnum one(); //... Definition here }; //Class is fully defined after here inline static constexpr const...
c++,language-lawyer,c++14,constexpr,constant-expression
This bullet was added by defect report 699 and it requires that a constexpr function or constructor must be defined before use. The defect report added the following example to 7.1.5 to demonstrate the rule: constexpr int square(int x); //OK, declaration constexpr struct pixel { // error: pixel is a...
The reason is that inside a constexpr function, parameters aren't constant expressions, regardless of whether the arguments are. You can call constexpr functions inside others, but the parameters of a constexpr function aren't constexpr inside, making any function call (even to constexpr functions) not a constant expression - inside. const...
c++,gcc,intel,static-members,constexpr
Because the function f takes a reference argument, there has to be a definition of S::ce that a reference can point to at runtime; the compiler can't just replace the argument with a literal 42. So you have to add an out-of-class definition: const int S::ce; just like you would...
no you can't do it, here's what the standard says (section 7.1.5): 1 The constexpr specifier shall be applied only to the definition of a variable or variable template, the declaration of a function or function template, or the declaration of a static data member of a literal type (3.9)....
The simple answer would probably be to simply add another static_assert: template<size_t ... Sizes> struct Test { static constexpr size_t Final = check_sizes<Sizes...>(); static_assert(Final > 0, ""); }; This will lead to two separate static assertion failures, though. If that is a problem for you, you could make sure check_sizes,...
TL;DR clang is correct, this is known gcc bug. You can either use intptr_t instead and cast when you need to use the value or if that is not workable then both gcc and clang support a little documented work-around that should allow your particular use case. Details So clang...
c++,templates,c++11,constexpr,typeid
Summarizing comments: boost::fusion::set<> has the functionality you need, with metafunctions to mimic most algorithms in the standard library, including insertion and removal. All Fusion containers are completely stack-based, as all are simply tuples with algorithm metafunctions on top. (Boost.Fusion overview here.)...
No, and your compiler already gave you a comprehensive explanation. But you could do this: constexpr char constString[] = "constString"; At runtime, this can be used to construct a std::string when needed....
c++,c++11,compile-time,constexpr,logarithm
Due to the confusion caused by the initial question I chose to post this answer. This is built upon the answers of @DanielKO and @Henrik. The minimum number of bits needed to encode n different states: constexpr unsigned bitsNeeded(unsigned n) { return n <= 1 ? 0 : 1 +...
c++,language-lawyer,c++14,initializer-list,constexpr
The standard committee seems to intend on initializer_list being a literal type. However, it doesn't look like it's an explicit requirement, and seems to be a bug in the standard. From § 3.9.10.5: A type is a literal type if it is: - a class type (Clause 9) that has...
Current constexpr support in the Standard Library is indeed rather limited. non-member std::begin is not marked constexpr because apart from array and initializer-list, no Standard Container (or container like entity such as bitset) supports constexpr member begin() (mainly because some implementations want to use iterator debugging using dynamic memory allocation)....
c++,arrays,c++14,constexpr,c++1z
To ensure that constexpr functions are evaluated at compile time, you must force them to be by making their result constexpr. For example: #include <array> int main() { constexpr std::array<int, 5> arr{1, 2, 3, 4, 5}; int i = arr[6]; // run time error } However: #include <array> int main()...
A constexpr function and a constexpr variable are related, but different things. A constexpr variable is a variable whose value is guaranteed to be available at compile time. A constexpr function is a function that, if evaluated with constexpr arguments, and behaves "properly" during its execution, will be evaluated at...
c++,arrays,c++11,copy-constructor,constexpr
You can use std::array. Since it is an aggregate type I believe this will work.
c++,c++11,auto,c++14,constexpr
If you consider this statement in [dcl.constexpr]/7: A call to a constexpr function produces the same result as a call to an equivalent non-constexpr function in all respects except that a call to a constexpr function can appear in a constant expression. Consider the non-constexpr equivalent function A::one(). _data[a] cannot...
So who is right here? Clang is correct in rejecting the code. [expr.const]/2: A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions: an invocation of a function other than a...
C++ standard section § 7.1.5 [dcl.constexpr] The definition of a constexpr function shall satisfy the following constraints: — it shall not be virtual (10.3); — its return type shall be a literal type; — each of its parameter types shall be a literal type; And section § 3.9 [basic.types] A...
c++11,const,immutability,constexpr
He states in the beginning of the section: C++ supports two notions of immutability and he lists const and constexpr, I don't believe he is attempting to say that constexpr ensures immutability better than const they just have different features, although I admit the fact the sentence cites section 10.4...
It's not contradictory. As well as mandating that the return type must be of "literal type", the draft standard states that a call to a constexpr function does not have to appear in a constant expression. From the C++11 draft standard: §7.1.5/7 A call to a constexpr function produces the...
c++,floating-point,g++,constexpr,builtin
I have an idea of another workaround. There is: use a helper function pass_through template<typename T> constexpr T&& pass_through (T&& t) { return static_cast<T&&>(t); } use it like this: static constexpr double value () { return 1.23; } static constexpr double result = __builtin_round(pass_through(__builtin_sqrt(value()))); This code is compiled without error...
c++11,const,clang,constexpr,c++14
I believe it's telling you that the member function can't be called on a const object as of C++1y. Add const after getSize() to make it a const member function: constexpr size_t getsize() const { ... } ...
c++,ternary-operator,c++14,constexpr
The resulting type from evaluating a ternary expression is the common type of its second and third arguments. By having the compiler deduce the return type, you force it to evaluate both of these arguments to the ternary expression. This means that the recursion doesn't end even when the terminating...
This is not feasible at compile time: Objects such as myTest are instantiated at runtime. Objects might be passed by reference or value to functions. When compiling the function, the compiler can't know for sure which was the original object it is referd to. If your state would be object...
c++,templates,c++14,constexpr,expression-templates
For the operator() in base to do a valid static_cast, the most-derived object that this points to must be of type Derived (or a subclass thereof). However, the members of e are of type base<derived>, not derived itself. In the line const noexcept { return m_a() + m_b(); } m_a...
c++,inheritance,c++11,constexpr
So "Well, inheriting ctors doesn't propagate constexpr, or something like that" is what I thought That's not the issue; default and copy/move constructors cannot be inherited. If you don't explicitly define or default them, they'll be implicitly defined following the usual rules. §12.9 [class.inhctor] 3 For each non-template...
c++,visual-c++,c++11,visual-studio-2013,constexpr
By the tables and explanation from Stephan T. L. in this blog, the constexpr is indeed only partially implemented in VS Nov 2013 CTP. The CTP supports C++11 constexpr, except for member functions. (Another limitation is that arrays aren't supported.) Also, it doesn't support C++14's extended constexpr rules. (wish to...
The place you define a constexpr function affects how you can use it. In particular: C++14[expr.const]p2: A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions: … an invocation of an undefined...
c++,language-lawyer,c++14,constexpr,lvalue-to-rvalue
This is because y.n is not odr-used and therefore does not require an access to y.n the rules for odr-use are covered in 3.2 and says: A variable x whose name appears as a potentially-evaluated expression ex is odr-used unless applying the lvalue-to-rvalue conversion (4.1) to x yields a constant...
template<size_t S> using size=std::integral_constant<size_t, S>; template<class T, size_t N> constexpr size<N> length( T const(&)[N] ) { return {}; } template<class T, size_t N> constexpr size<N> length( std::array<T, N> const& ) { return {}; } template<class T> using length_t = decltype(length(std::declval<T>())); constexpr size_t sum_string_sizes() { return 0; } template<class...Ts> constexpr size_t...
In C++, the error is not the call to arraySize(y), but the declaration of y itself. The bounds in an array declaration must be a "converted constant expression". If your compiler accepts the declaration of y and later tells you that y is an array of runtime bound, it is...
myStrlen(pruVar) can be evaluated at compile time; the compiler is just choosing not to in this instance. If you want to force the compiler to evaluate it at compile time or error if this is not possible, assign the result to a constexpr variable: constexpr unsigned int size = myStrlen(pruVar);...
The issue is that in a variable declaration, constexpr always applies the const-ness to the object declared; const on the other hand can apply to a different type, depending on the placement. Thus constexpr const int i = 3; constexpr int i = 3; are equivalent; constexpr char* p =...
arrays,c++11,lambda,c++14,constexpr
C++11 [expr.prim.lambda]/12 If a lambda-expression odr-uses this or a variable with automatic storage duration from its reaching scope, that entity shall be captured by the lambda-expression. and /17 Every id-expression that is an odr-use of an entity captured by copy is transformed into an access to the corresponding unnamed data...
c++,memory,constructor,shared-ptr,constexpr
The rules on constexpr constructors changed between C++11 and C++14; see DR1911 constexpr constructor with non-literal base class and this bug. The fix is to compile in C++14 mode (-std=c++14). Language in C++11 [dcl.constexpr]: For a constexpr function, if no function argument values exist such that the function invocation substitution...
c++,linker,c++14,constexpr,perfect-forwarding
Because you are taking a reference the variable is odr-used and this requires a definition out of line: constexpr const char* SomeStruct::someString; see it working live. From the draft C++14 standard section 3.2 [basic.def.odr]: A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless...
1. Short answer: It works irrespective of it being declared constexpr, because you're defining an object with static storage duration (that is not a string literal - it stores a copy of the contents of one), and its address is a constant expression. Regarding linkage, str2 has internal linkage, but...
The difference is described in the following quote from the C++ Standard (9.4.2 Static data members) 3 If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignmentexpression is a...
You may specialize the method : template<unsigned C, unsigned M> struct MyClass { static constexpr int mode(); }; template<> constexpr int MyClass<1, 1>::mode() { return 101; } template<> constexpr int MyClass<1, 2>::mode() { return 21; } And with TAG(N, C) from Macro for mapping integer to string for token: #define...
c++,arrays,c++11,language-lawyer,constexpr
It's a bug and appears to compile on gcc 5 as shown here.
A declaration of a static data member in class is never a definition1. A definition is necessary whenever a variable is odr-used2. std::get<> takes arguments per reference, and binding a variable to a reference odr-uses it immediately3. Simply define names outside: constexpr T::Names T::names; // Edit: This goes *outside* the...
c++,templates,pointers,constexpr,compile-time-constant
The compiler doesn't need to know the value of &a at compile time any more than it needs the value of function addresses. Think of it like this: the compiler will instantiate your function template with &a as a parameter and generate "object code" (in whatever format it uses to...
c++,templates,c++11,language-lawyer,constexpr
It looks like this restriction was subject to the following proposal Allow constant evaluation for all non-type template arguments, still trying to determine the status of this proposal. It says: The syntactic restrictions for pointers, references, and pointers to members are awkward and prevent reasonable refactorings. For instance: template<int *p>...
The expression lhs |= 1u << static_cast<uint8_t>(rhs) can never be a constant expression itself, because it modifies lhs. The rule that forbids this in C++14 is §5.19/2.15 (an effectively equivalent one exists in C++11 as well): A conditional-expression e is a core constant expression unless the evaluation of e, following...
You are missing an initializer on iseq. You have to add it: constexpr std::integer_sequence<int, 1,2,3,4> iseq{}; ^^ From [dcl.constexpr]: A constexpr specifier used in an object declaration declares the object as const. Such an object shall have literal type and shall be initialized. If it is initialized by a constructor...
c++,c++11,visual-c++,constexpr,visual-studio-2015
If the compiler accepts a constexpr member function static constexpr T value() {return 42;} then that should give you a compile-time constant. DISCLAIMER: I never use this compiler, so can't test this....
An object or function must be defined if it is odr-used. In some cases objects and functions are not odr-used and in those cases you don't have to define them. Whether or not the declaration of a static class member has an initializer, it is still not a definition. In...
Before constexpr the compilers could sometimes figure out a compile time constant and use it. However, the programmer could never know when this would happen. Afterwards, the programmer is immediately informed if an expression is not a compile time constant and he or she realizes the need to fix it....
c++,c++11,static-variables,constexpr
The main difference that I know is, the value of constexpr must be known in compile-time while a const static can be assigned in run-time. const static int x = rand(); ...
C++11 FD: A variable whose name appears as a potentially-evaluated expression is odr-used unless it is an object that satisfies the requirements for appearing in a constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) is immediately applied. Clearly the l-t-r conversion is immediately applied. So can a variable of floating...
As @Casey correctly pointed out in the comments, there is nothing foggy about the constexpr-ness of the implicit constructor of std::array or other aggregates: 12.1 Constructors [class.ctor] 5 A default constructor that is defaulted and not defined as deleted is implicitly defined when it is odr-used (3.2) to create an...
wouldn't this work for your problem: template <typename T> struct Dummy { template <T t> friend EnumValueWrapper<T, t> makeEnumValueWrapper(); }; template struct Dummy<E1>; typedef ClassTemplatedOnAnyValueNoMatterType<decltype(makeEnumValueWrapper<E1::First>())> MyClass; you still need the explicit instantiation of the Dummy on each of the relevant classes, but the typedef for MyClass is free of redundant...
Infinity is an implementation defined result, the standard does not require IEEE floating point and division by zero is formally undefined behavior and constant expression have an exclusion for undefined behavior. From the draft C++ standard section 5.6 [expr.mul]: The binary / operator yields the quotient, and the binary %...