constexpr

adapting a non-constexpr integral value to a non-type template parameter, and code bloat

限于喜欢 提交于 2019-12-01 05:53:56
Consider a function object F taking a constexpr size_t argument I struct F { template <size_t I> constexpr size_t operator()(size <I>) const { return I; } }; wrapped within a type size <I> , where (for brevity) template <size_t N> using size = std::integral_constant <size_t, N>; Of course, we could pass I directly but I want to emphasize that it is constexpr by using it as a template argument. Function F is dummy here but in reality it could do a variety of useful stuff like retrieving information from the I th element of a tuple. F is assumed to have the same return type regardless of I . I

What means “obey ODR” in case of inline and constexpr function?

落花浮王杯 提交于 2019-12-01 04:51:31
问题 I just read that constexpr and inline functions obey one-definition rule, but they definition must be identical. So I try it: inline void foo() { return; } inline void foo() { return; } int main() { foo(); }; error: redefinition of 'void foo()', and constexpr int foo() { return 1; } constexpr int foo() { return 1; } int main() { constexpr x = foo(); }; error: redefinition of 'constexpr int foo()' So what exactly means that, constexpr and inline function can obey ODR? 回答1: I just read that

Compiler error when initializing constexpr static class member

一笑奈何 提交于 2019-12-01 04:34:00
I've declared a class in the following way class A { struct B { constexpr B(uint8_t _a, uint8_t _b) : a(_a), b(_b) {} bool operator==(const B& rhs) const { if((a == rhs.a)&& (b == rhs.b)) { return true; } return false; } uint8_t a; uint8_t b; }; constexpr static B b {B(0x00, 0x00)}; }; But g++ says error: field initializer is not constant Can't figure out where I'm wrong. Clang is more helpful: 27 : error: constexpr variable 'b' must be initialized by a constant expression constexpr static B b {B(0x00, 0x00)}; ^~~~~~~~~~~~~~~~ 27 : note: undefined constructor 'B' cannot be used in a constant

How to print result of a compile-time calculation in C++?

有些话、适合烂在心里 提交于 2019-12-01 03:48:07
I've wrote several constexpr functions and use them in static_asserts to control some resource limits. But I'd like to not only enforce compile-time predicate but also to see the actual values calculated during normal compilation process or at least when assertion fails. There are ways to print string messages during compilation, but what's about printing results of constexpr computations? Here is some code that exploits gcc's diagnostic messages to print values of interest after an assert message. To find the values of interest, you just need to search the error string for T x = : #include

static constexpr pointer-to-function, difference between compilers

左心房为你撑大大i 提交于 2019-12-01 03:34:28
When answering this question , I tried the following code with gcc ( code compiled ) and clang ( code rejected ): typedef long (*func)(int); long function(int) { return 42; } struct Test { static constexpr func f = &function; }; template<func c> struct Call { static void f() { c(0); } }; int main() { Call<Test::f>::f(); } I am not sure which compiler is right, although I think the constexpr initialization of Test::f is ok. The error clang outputs is: error: non-type template argument for template parameter of pointer type 'func' (aka 'long (*)(int)') must have its address taken Which compiler

C++0x error with constexpr and returning template function

▼魔方 西西 提交于 2019-12-01 03:17:00
I tried to find a solution for the problem of the question C++ template non-type parameter type deduction , which does not involve a template parameter to call f, but implicitly chooses the correct type for the template parameter. Since constexpr should guarantee that a function only contains compile time constants, and is evaluated at compile time (at least thats what i think it does), i thought it might be the solution for this issue. So i came up with this: template <class T, T VALUE> void f() {} //first i tried this: template <class T> auto get_f(T t) -> decltype( &f<T,t> ) { return f<T,t>

Is it legal to use side-effects in exceptions thrown by constexpr?

邮差的信 提交于 2019-12-01 03:06:49
Normally, constexpr must be free of side-effects. However, I just discovered that it is possible to use side-effects in the constructors of thrown exceptions. That technique can be used to emulate assert() for constexpr functions, as it is demonstrated in the following program. #include <iostream> #include <cstdlib> #include <stdexcept> struct constexpr_precond_violated : std::logic_error { constexpr_precond_violated(const char* msg) : std::logic_error(msg) { std::cerr << msg << '\n'; abort(); // to get a core dump } }; #define TO_STRING_IMPL(x) #x #define TO_STRING(x) TO_STRING_IMPL(x)

What is the use of a constexpr on a non-const member function?

懵懂的女人 提交于 2019-12-01 02:35:16
The accepted answer in literal class compile error with constexpr constructor and function (differ vc, g++) shows that in C++14 there is a difference in the way constexpr int A::a() and constexpr A::a() const can be used. i.e. constexpr on a member function does not imply that the function does not change the object it acts on. The given example is: struct A { constexpr A() {} constexpr int a() {return 12; } constexpr int b() const {return 12; } }; int main() { constexpr A a; // DOES NOT COMPILE as a() is not const // constexpr int j = a.a(); const int k = a.b(); // Fine since b() is const }

Possible to instantiate templates using a for loop in a C++14 constexpr function?

China☆狼群 提交于 2019-12-01 02:28:42
I've been messing around with an SVN build of clang to experiment with the relaxed rules for constexpr . One of the things I've been unable to determine as of yet is whether it's possible to loop through the elements inside a tuple at compile-time in a constexpr function. Because I don't have a C++14 compliant standard library to test on, I've prepared the following equivalent test: template<int N> constexpr int foo() { return N; } constexpr int getSum() { auto sum = 0; for (auto i = 0; i < 10; ++i) { sum += foo<i>(); } return sum; } constexpr auto sum = getSum(); The interesting part here is

lambda as a static member

自作多情 提交于 2019-12-01 02:07:34
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 a constant expression static constexpr auto F = [](){}; ^~~~~~ It seems in clang, lambdas aren't considered a constant expression. Is this correct? Perhaps they haven't fully implemented lambdas yet in clang because gcc 4.7 seems to allow it as a constexpr , but it give another error: error: ‘constexpr const<lambda()> A::F’, declared using local