constexpr

Why aren't std::algorithms constexpr and which could be?

家住魔仙堡 提交于 2019-12-01 02:05:38
Why aren't any std::algorithm methods constexpr ? If I understand the new C++14 rules correctly, many of these methods could be constexpr . For example, why can't std::find be constexpr ? static constexpr std::array<char, 4> DnaBases {'A', 'C', 'G', 'T'}; constexpr bool is_dna(char b) { return std::find(std::cbegin(DnaBases), std::cend(DnaBases), b) != std::cend(DnaBases); // why not? } Which other std::algorithm s could be constexpr ? ForEveR It could be constexpr , but cannot be evaluated as a constant expression, since in this case, for example for compile-time find it is required that:

Is this constexpr integer not a null pointer constant?

吃可爱长大的小学妹 提交于 2019-12-01 00:53:33
问题 Consider the following C++11 program, and its result in GCC 4.7.2: int main() { constexpr int i = 0; int* p = i; } // g++ -g -ggdb -Wall -Wextra -pedantic -std=c++11 t.cpp // t.cpp: In function 'int main()': // t.cpp:4:13: error: invalid conversion from 'int' to 'int*' [-fpermissive] // t.cpp:4:9: warning: unused variable 'p' [-Wunused-variable] According to the standard: [C++11: 4.10/1]: A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates

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

纵然是瞬间 提交于 2019-11-30 22:01:22
问题 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

Understanding static constexpr member variables

这一生的挚爱 提交于 2019-11-30 21:55:49
问题 I have some confusions regarding static constexpr member variables in C++11. In first.hpp template<typename T> struct cond_I { static constexpr T value = 0; }; // specialization template<typename T> struct cond_I< std::complex<T> > { static constexpr std::complex<T> value = {0,1}; }; In main() function cout << cond_I<double>::value << endl; // this works fine cout << cond_I< complex<double> >::value << endl; // linker error However if I add the following line to first.hpp everything works

Assign static constexpr class member to runtime variable

我怕爱的太早我们不能终老 提交于 2019-11-30 19:33:56
I know there are a lot of similar questions, but somehow different questions. It is about the following situation: #include <iostream> #include <array> template<typename T> class MyClass { public: static constexpr std::array<T,4> ARRAY {{4, 3, 1, 5}}; }; int main() { constexpr std::array<int, 4> my_array(MyClass<int>::ARRAY); // works fine -> can use the ARRAY to initialize constexpr std::array constexpr int VALUE = 5*MyClass<int>::ARRAY[0]; // works also fine int value; value = my_array[0]; // can assign from constexpr value = MyClass<int>::ARRAY[0]; // undefined reference to `MyClass<int>:

Compile time computing of number of bits needed to encode n different states

淺唱寂寞╮ 提交于 2019-11-30 18:55:12
Edit: In the initial question had a wrong formula and the algorithm tried was doing something completely different than what was intended. I apologise and I decided to rewrite the question to eliminate all the confusion. I need to compute at compile time (the result will be used as a non-type template parameter) the minimum number of bits needed to store n different states: constexpr unsigned bitsNeeded(unsigned n); or via template The results should be: +-----------+--------+ | number of | bits | | states | needed | +-----------+--------+ | 0 | 0 | * or not defined | | | | 1 | 0 | | | | | 2 |

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

ⅰ亾dé卋堺 提交于 2019-11-30 18:50:44
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 constexpr Online Compiler URL: http://goo.gl/jni6Em Compiler: clang 3.4 (with -std=c++1y) System: Linux 3.2 If I delete L2, this code compiles. If I add L2, the compiler complained "modification of object of const-qualified type 'const int' is not allowed in a constant expression". I am not a language lawyer, so I am not sure whether this is true. However, if it is, why compiler didn't

Undefined symbols for constexpr function

江枫思渺然 提交于 2019-11-30 17:41:11
When I attempt compiling the following code I get a linker error: Undefined symbols for architecture x86_64: "Foo()", referenced from: _main in main.o using LLVM 4.2. This behavior only occurs when the function is marked constexpr . The program compiles and links correctly when the function is marked const . Why does declaring the function constexpr cause a linker error? (I realize that writing the function this way doesn't give the benefit of compile-time computation; at this point I am curious why the function fails to link.) main.cpp #include <iostream> #include "test.hpp" int main() { int

Why is std::array::size constexpr with simple types (int, double, …) but not std::vector (GCC)?

会有一股神秘感。 提交于 2019-11-30 17:24:36
The following code: std::array<int, 4> arr1; std::array<float, arr1.size()> arr2; ...compiles with both gcc and clang because std::array::size is considered constexpr . But the following does not compile with gcc (version 5.3.0 20151204): std::array<std::vector<int>, 4> arr1; std::array<std::vector<double>, arr1.size()> arr2; For me, there is no reason such code should fail to compile if the first one works but since I did not find a lot of post on this I don't know if it is a gcc bug or a clang extension? The error from gcc (that I don't really understand... ): main.cpp: In function 'int main

Why is this not a constant expression?

落爺英雄遲暮 提交于 2019-11-30 17:09:37
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[i]>::value; } int main() { char a = test1("Test", 0); //Compiles OK char b = test2("Test", 0); //error: