constexpr

Why do I need to member-initialize a non-static array member in a constexpr class?

Why do I need to member-initialize a non-static array member in a constexpr class?
The following code desperately needs: values()to compile, at least in ideone::C++14:#include <iostream> template<int N> struct Table { constexpr Table() : values() { for (auto i = 0; i < N; ++i) { values[i] = i * i * i; } } int values[N]; }

Using a lambda in a `constexpr` function in a non-`constexpr` context: clang vs gcc

Using a lambda in a `constexpr` function in a non-`constexpr` context: clang vs gcc
Consider the following code(available on gcc.godbolt.org):template <typename TF> constexpr auto fn_x(TF f) { return f(); } constexpr auto get_x() { return fn_x([]{ return 0; }); } int main() { auto res = get_x(); }It compiles underg++ 5.3.xand newer

Purpose of constexpr

Purpose of constexpr
This is more of a philosophical question rather than practical code snippet, but perhaps C++ gurus can enlighten me (and apologies if it's been asked already).I have been reading Item 15 in Meyers's "Effective Modern C++" book, as well as this t

C++ : Using constexpr to alias template parameters, causing function definitions to not match to declarations

C++ : Using constexpr to alias template parameters, causing function definitions to not match to declarations
I'm trying to get something like template forwarding based on a precompiler variable. However, I'm running into strange compile errors, so I'll try to give an example of what I'm working on.I have a matrix class that takes two template parameters, an

Can virtual functions be constexpr?

Can virtual functions be constexpr?
Can virtual functions likeX::f()in the following codestruct X { constexpr virtual int f() const { return 0; } };beconstexpr?No. From [dcl.constexpr]/3 (7.1.5, "Theconstexprspecifier"):The definition of aconstexprfunction shall satisfy the follow

How to tell static_assert that constexpr function arguments are const?

How to tell static_assert that constexpr function arguments are const?
I have a constexpr function that looks something like this:constexpr int foo(int bar) { static_assert(bar>arbitrary_number, "Use a lower number please"); return something_const; }However, compiling this with GCC 4.6.3 keeps telling meerror: '

constexpr replacements for math constants like M_PI

constexpr replacements for math constants like M_PI
Are there any C++11constexprconstants which can be used instead of the constant macros from<cmath>, i.e. constants likeM_PIand friends? Or lacking that, any globalconstvalues which provide these constants at runtime?There are no predefined constexpr

Could non-static member variable be modified in constexpr constructor (C++14)?

Could non-static member variable be modified in constexpr constructor (C++14)?
struct A { int a = 0; constexpr A() { a = 1; } }; constexpr bool f() { constexpr A a; static_assert(a.a == 1, ""); // L1: OK return a.a == 1; } static_assert(f(), ""); // L2: Error, can not modify A::a in constexprOnline Compiler URL:h

When should you use constexpr capability in C++11?

When should you use constexpr capability in C++11?
It seems to me that having a "function that always returns 5" is breaking or diluting the meaning of "calling a function". There must be a reason, or a need for this capability or it wouldn't be in C++11. Why is it there?// preprocesso

C++ Linker Error With Class static constexpr

C++ Linker Error With Class static constexpr
I am compiling the following simple program withg++-4.6.1 --std=c++0x:#include <algorithm> struct S { static constexpr int X = 10; }; int main() { return std::min(S::X, 0); };I get the following linker error:/tmp/ccBj7UBt.o: In function `main': scra

What's happening with this code in gcc-4.8? [closed]

What's happening with this code in gcc-4.8? [closed]
Errors:main.cpp: In function 'constexpr int fib(int)': main.cpp:6:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] return (n < sizeof(ftbl)/sizeof(*ftbl)) ? ftbl[n] : fib(n-2) + fib(n-1); ^ /tmp/cch0aLwI.o: In

SFINAE doesn't work on a constexpr function?

SFINAE doesn't work on a constexpr function?
To support portability I want to choose a constant based on the fact whethersize_tis 32 bit or 64 bit. The code:using namespace std; namespace detail { template<enable_if<is_same<size_t, uint32_t>::value,void*>::type = nullptr> constexpr

Getting around the reinterpret cast limitation with constexpr

Getting around the reinterpret cast limitation with constexpr
In c++11, aconstexprexpression cannot contain reinterpret casts. So for instance, if one wanted to manipulate the bits in a floating point number, say to find the mantissa of the number:constexpr unsigned int mantissa(float x) { return ((*(unsigned i

g++ doesn't compile constexpr function with assert in it

g++ doesn't compile constexpr function with assert in it
template<typename T> constexpr inline T getClamped(const T& mValue, const T& mMin, const T& mMax) { assert(mMin < mMax); // remove this line to successfully compile return mValue < mMin ? mMin : (mValue > mMax ? mMax : mValue);

lambda as a static member

lambda as a static member
I'm trying to use a lambda as a static member, like this:struct A { static constexpr auto F = [](){}; }; int main() { A::F(); return 0; }Is this even correct C++11 code? On clang, I get this error:error: constexpr variable 'F' must be initialized by

C++11 constexpr variadic logic expression

C++11 constexpr variadic logic expression
I was trying to use variadics templates for test certain propierties (type_trait) at compile time, but seems the following code can't compiletemplate<typename test> constexpr bool trait_test(){ return test::value; } template<typename test, typena

constexpr template argument weirdness

constexpr template argument weirdness
GCC (5.3) & Clang (3.8) claim that the first line intestis bad, but the second one is fine. MSVC (2015.2) says, both are invalid.template< typename N, typename T > void f( N n, T t ) { std::get< n >( t ); } void test() { std::get< std::

constexpr string literals in anonymous namespace?

constexpr string literals in anonymous namespace?
I have the following example code which uses a string literal as a template parameter, such that the base class template can access the string.The code compiles, but I get a warning which I do not fully understand:warning: 'ns::bar::type' has a base
What Others Are Reading