constexpr

static constexpr pointer-to-function, difference between compilers

匆匆过客 提交于 2019-12-19 05:45:41
问题 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

Why isn't abs constexpr?

℡╲_俬逩灬. 提交于 2019-12-18 18:57:25
问题 In <cinttypes> , since C++11, there are the following two overloads: std::intmax_t abs( std::intmax_t n ); std::intmax_t imaxabs( std::intmax_t n ); Why aren't those two functions constexpr ? 回答1: I can't give a good reason for why abs couldn't be constexpr and apparently neither can gcc . When I use gcc 4.9.2 with this program: #include <cstdlib> #include <cinttypes> #include <cassert> constexpr intmax_t abs3 = std::abs(3); constexpr intmax_t absneg3 = std::abs(-3); int main() { assert(abs3

Why is this not a constant expression?

▼魔方 西西 提交于 2019-12-18 18:46:49
问题 In this trivial example, test2 fails to compile even though test1 succeeds, and I don't see why that is the case. If arr[i] is suitable for a return value from a function marked constexpr then why can it not be used as a non-type template argument? template<char c> struct t { static const char value = c; }; template <unsigned N> constexpr char test1(const char (&arr)[N], unsigned i) { return arr[i]; } template <unsigned N> constexpr char test2(const char (&arr)[N], unsigned i) { return t<arr

User defined literal arguments are not constexpr?

假装没事ソ 提交于 2019-12-18 14:06:05
问题 I'm testing out user defined literals. I want to make _fac return the factorial of the number. Having it call a constexpr function works, however it doesn't let me do it with templates as the compiler complains that the arguments are not and cannot be constexpr . I'm confused by this - aren't literals constant expressions? The 5 in 5_fac is always a literal that can be evaluated during compile time, so why can't I use it as such? First method: constexpr int factorial_function(int x) { return

How undefined are __builtin_ctz(0) or __builtin_clz(0)?

落爺英雄遲暮 提交于 2019-12-18 11:06:08
问题 Background For a long time, gcc has been providing a number of builtin bit-twiddling functions, in particular the number of trailing and leading 0-bits (also for long unsigned and long long unsigned , which have suffixes l and ll ): — Built-in Function: int __builtin_clz (unsigned int x) Returns the number of leading 0-bits in x , starting at the most significant bit position. If x is 0, the result is undefined. — Built-in Function: int __builtin_ctz (unsigned int x) Returns the number of

How undefined are __builtin_ctz(0) or __builtin_clz(0)?

倾然丶 夕夏残阳落幕 提交于 2019-12-18 11:05:49
问题 Background For a long time, gcc has been providing a number of builtin bit-twiddling functions, in particular the number of trailing and leading 0-bits (also for long unsigned and long long unsigned , which have suffixes l and ll ): — Built-in Function: int __builtin_clz (unsigned int x) Returns the number of leading 0-bits in x , starting at the most significant bit position. If x is 0, the result is undefined. — Built-in Function: int __builtin_ctz (unsigned int x) Returns the number of

Guidelines to do constexpr operator-overloading?

馋奶兔 提交于 2019-12-18 10:50:09
问题 Consider a simple int Wrapper class with overloaded multiplication operator*= and operator* . For "old-style" operator-overloading, one can define operator* in terms of operator*= , and there are even libraries like Boost.Operators and its modern incarnation df.operators by @DanielFrey that reduce the boilerplate for you. However, for compile-time computations using the new C++11 constexpr , this convenience disappears. A constexpr operator* cannot call operator*= because the latter modifies

Why can't non-static data members be constexpr?

白昼怎懂夜的黑 提交于 2019-12-18 10:41:49
问题 This is valid code: struct S { constexpr S(int x, int y): xVal(x), yVal(y) {} constexpr S(int x): xVal(x) {} constexpr S() {} const int xVal { 0 }; const int yVal { 0 }; }; But here I'd really like to declare xVal and yVal constexpr --like this: struct S { constexpr S(int x, int y): xVal(x), yVal(y) {} constexpr S(int x): xVal(x) {} constexpr S() {} constexpr int xVal { 0 }; // error! constexpr int yVal { 0 }; // error! }; As indicated, the code won't compile. The reason is that (per 7.1.5/1)

C++11 enum with class members and constexpr link-time optimization

…衆ロ難τιáo~ 提交于 2019-12-18 08:35:46
问题 In my project I have a lot of enumerations that need to have additional attributes associated with the enumeration members and auxiliary static methods associated with the enumeration type. As much as I know, this is not possible to have with the standard enum class MyItem {...}, so for each enum class in my project I have an auxiliary class MyItemEnum that encapsulates these auxiliary static methods and also instantiates auxiliary instances of itself, so that I can access their methods in

C++11 enum with class members and constexpr link-time optimization

╄→гoц情女王★ 提交于 2019-12-18 08:34:20
问题 In my project I have a lot of enumerations that need to have additional attributes associated with the enumeration members and auxiliary static methods associated with the enumeration type. As much as I know, this is not possible to have with the standard enum class MyItem {...}, so for each enum class in my project I have an auxiliary class MyItemEnum that encapsulates these auxiliary static methods and also instantiates auxiliary instances of itself, so that I can access their methods in