constexpr

constexpr static member before/after C++17

孤者浪人 提交于 2019-11-26 17:47:10
问题 As far as I can see, a very common situation is something like template<int i> class Class { public: static constexpr int I = i; static constexpr int J = constexprFunction(i); // further Class implementation }; Almost as common I see the mistake (in fact most of my questions here are because I forgot it and did not know, what the proper question had been) to forget the additional definition if the member are odr-used: template<int i> constexpr int Class<i>::I; template<int i> constexpr int

static constexpr member of same type as class being defined

孤者浪人 提交于 2019-11-26 17:42:14
问题 I would like a class C to have a static constexpr member of type C. Is this possible in C++11? Attempt 1: struct Foo { constexpr Foo() {} static constexpr Foo f = Foo(); }; constexpr Foo Foo::f; g++ 4.7.0 says: 'invalid use of incomplete type' referring to the Foo() call. Attempt 2: struct Foo { constexpr Foo() {} static constexpr Foo f; }; constexpr Foo Foo::f = Foo(); Now the problem is the lack of an initializer for the constexpr member f inside the class definition. Attempt 3: struct Foo

constexpr overloading

心已入冬 提交于 2019-11-26 17:31:16
Related: Function returning constexpr does not compile I feel like constexpr is limited in usefulness in C++11 because of the inability to define two functions that would otherwise have the same signature, but have one be constexpr and the other not constexpr. In other words, it would be very helpful if I could have, for example, a constexpr std::string constructor that takes constexpr arguments only, and a non-constexpr std::string constructor for non-constexpr arguments. Another example would be a theoretically complicated function that could be made more efficient by using state. You can't

Computing length of a C string at compile time. Is this really a constexpr?

浪尽此生 提交于 2019-11-26 17:27:26
I'm trying to compute the length of a string literal at compile time. To do so I'm using following code: #include <cstdio> int constexpr length(const char* str) { return *str ? 1 + length(str + 1) : 0; } int main() { printf("%d %d", length("abcd"), length("abcdefgh")); } Everything works as expected, the program prints 4 and 8. The assembly code generated by clang shows that the results are computed at compile time: 0x100000f5e: leaq 0x35(%rip), %rdi ; "%d %d" 0x100000f65: movl $0x4, %esi 0x100000f6a: movl $0x8, %edx 0x100000f6f: xorl %eax, %eax 0x100000f71: callq 0x100000f7a ; symbol stub for

Calling constexpr in default template argument

眉间皱痕 提交于 2019-11-26 17:23:33
问题 In C++11 I am using a constexpr function as a default value for a template parameter - it looks like this: template <int value> struct bar { static constexpr int get() { return value; } }; template <typename A, int value = A::get()> struct foo { }; int main() { typedef foo<bar<0>> type; return 0; } G++ 4.5 and 4.7 compiles this, but Clang++ 3.1 does not. The error message from clang is: clang_test.cpp:10:35: error: non-type template argument is not a constant expression template <typename A,

Why is this constexpr static member function not seen as constexpr when called?

为君一笑 提交于 2019-11-26 17:22:47
问题 Why is this constexpr static member function, identified by the //! Nah comment, not seen as constexpr when called? struct Item_id { enum Enum { size, position, attributes, window_rect, max_window_size, _ }; static constexpr int n_items_ = _; // OK constexpr auto member_n_items() const -> int { return _; } // OK static constexpr auto static_n_items() -> int { return _; } // OK static constexpr int so_far = n_items_; // OK #ifndef OUT_OF_CLASS static constexpr int bah = static_n_items(); //!

Why can I call a non-constexpr function inside a constexpr function?

一个人想着一个人 提交于 2019-11-26 16:35:08
问题 Consider the following code: #include <stdio.h> constexpr int f() { return printf("a side effect!\n"); } int main() { char a[f()]; printf("%zd\n", sizeof a); } I would have expected the compiler to complain about the call to printf inside f , because f is supposed to be constexpr , but printf is not. Why does the program compile and print 15? 回答1: The program is ill-formed and requires no diagnostic according to the C++11 draft standard section 7.1.5 The constexpr specifier paragraph 5 which

C++ Linker Error With Class static constexpr

耗尽温柔 提交于 2019-11-26 15:57:57
问题 I am compiling the following simple program with g++-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': scratch.cpp:(.text+0x17): undefined reference to `S::X' collect2: ld returned 1 exit status I realize that inline-defined static members do not have symbols defined, but I was under the (probably flawed) impression that using constexpr told the

Throw in constexpr function

故事扮演 提交于 2019-11-26 14:36:29
问题 The following piece of code compiles under clang++ 3.7.0 but is denied by g++ 5.3.1. Both have -std=c++14 option. Which compiler is correct? Anyone knows where in the standard talks about this? Thanks. #include <stdexcept> using namespace std; constexpr int f(int n) { if (n <= 0) throw runtime_error(""); return 1; } int main() { char k[f(1)]; } Output [hidden] g++ -std=c++14 c.cpp c.cpp: In function ‘constexpr int f(int)’: c.cpp:7:1: error: expression ‘<throw-expression>’ is not a constant

“constexpr if” vs “if” with optimizations - why is “constexpr” needed?

有些话、适合烂在心里 提交于 2019-11-26 14:18:45
问题 C++1z will introduce "constexpr if" - an if that will have one of branches removed, based on the condition. Seems reasonable and useful. However, is it not possible to do without constexpr keyword? I think that during compilation, compiler should know wheter condition is known during compilation time or not. If it is, even the most basic optimization level should remove unnecessary branch. For example (see in godbolt: https://godbolt.org/g/IpY5y5): int test() { const bool condition = true; if