constexpr

Assign static constexpr class member to runtime variable

女生的网名这么多〃 提交于 2019-12-30 06:23:12
问题 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];

Assign static constexpr class member to runtime variable

三世轮回 提交于 2019-12-30 06:23:05
问题 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 C++ constexpr function actually accept non-constant expression as argument?

一个人想着一个人 提交于 2019-12-30 04:28:21
问题 I have defined a constexpr function as following: constexpr int foo(int i) { return i*2; } And this is what in the main function: int main() { int i = 2; cout << foo(i) << endl; int arr[foo(i)]; for (int j = 0; j < foo(i); j++) arr[j] = j; for (int j = 0; j < foo(i); j++) cout << arr[j] << " "; cout << endl; return 0; } The program was compiled under OS X 10.8 with command clang++. I was surprised that the compiler did not produce any error message about foo(i) not being a constant expression

Why does the C++ compiler makes it possible to declare a function as constexpr, which can not be constexpr?

我只是一个虾纸丫 提交于 2019-12-29 08:51:58
问题 Why does the C++ compiler makes it possible to declare a function as constexpr, which can not be constexpr? For example: http://melpon.org/wandbox/permlink/AGwniRNRbfmXfj8r #include <iostream> #include <functional> #include <numeric> #include <initializer_list> template<typename Functor, typename T, size_t N> T constexpr reduce(Functor f, T(&arr)[N]) { return std::accumulate(std::next(std::begin(arr)), std::end(arr), *(std::begin(arr)), f); } template<typename Functor, typename T> T constexpr

Initializing a static constexpr data member of the base class by using a static constexpr data member of the derived class

浪尽此生 提交于 2019-12-29 06:43:14
问题 Consider the following code: template<typename T> struct S { static constexpr int bar = T::foo; }; struct U: S<U> { static constexpr int foo = 42; }; int main() { } GCC v6.1 compiles it, clang 3.8 rejects it with the error: 2 : error: no member named 'foo' in 'U' struct S { static constexpr int bar = T::foo; }; Which compiler is right? Could it be due to the fact that U is not a complete type at the point where we try to use it within S ? In this case, it should be considered a bug of GCC,

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

狂风中的少年 提交于 2019-12-29 04:25:44
问题 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); } error : body of constexpr function 'constexpr T getClamped(const T&, const T&, const T&) [with T = long unsigned int]' not a return-statement Using g++ 4.8.1 . clang++ 3.4 doesn't complain. Who is right here? Any way I can make g++ compile the code without using

How to generate a constexpr version string from three integers (or perhaps a git/SVN commit/rev. string)?

橙三吉。 提交于 2019-12-29 04:01:11
问题 Say I have constexpr const std::uint8_t major = 1; constexpr const std::uint8_t minor = 10; constexpr const std::uint8_t bugfix = 0; and I want constexpr const char* version_string(){ ... } to return the equivalent of "1.10.0" in this example, how would I do it? I assume I'll need both of these, in constexpr : integer to string conversion string concatenation The problem is purely academic, and I see little to no use to actually have it constexpr other than "it's possible". I just can't see

Can std::array be used in a constexpr class?

为君一笑 提交于 2019-12-28 16:31:12
问题 I am currently creating a class with a constexpr constructor and I wonder if I can use an std::array to store the data of this class. Does the standard explicitly specify that an std::array has a constexpr constructor and that its contents can be accessed at compile-time ? 回答1: Because std::array<T, N> is an aggregate, it can be initialized as a constexpr if and only if the underlying type T has a constexpr constructor (when presented with each initializer you provide). 回答2: Based on the

Using numeric_limits::max() in constant expressions

折月煮酒 提交于 2019-12-28 16:30:54
问题 I would like to define inside a class a constant which value is the maximum possible int. Something like this: class A { ... static const int ERROR_VALUE = std::numeric_limits<int>::max(); ... } This declaration fails to compile with the following message: numeric.cpp:8: error: 'std::numeric_limits::max()' cannot appear in a constant-expression numeric.cpp:8: error: a function call cannot appear in a constant-expression I understand why this doesn't work, but two things look weird to me: It

constexpr const vs constexpr variables? [duplicate]

落爺英雄遲暮 提交于 2019-12-28 05:35:28
问题 This question already has answers here : Difference between `constexpr` and `const` (8 answers) Closed 4 years ago . It seems obvious that constexpr implies const and thus it is common to see: constexpr int foo = 42; // no const here However if you write: constexpr char *const str = "foo"; Then GCC will spawn "warning: deprecated conversion from string constant to ‘char*’" if -Wwrite-string flag is passed. Writing: constexpr const char *const str = "foo"; solves the issue. So are constexpr