constexpr

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

左心房为你撑大大i 提交于 2019-11-29 05:26:14
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 ? 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). Based on the comment by @MarkGlisse: this compiles #include <array> #include <iostream> template<typename T, std::size_t N> struct

Static templated constexpr nested class member

跟風遠走 提交于 2019-11-29 03:46:20
I have the following sample class Foo with nested class Bar and everything is constexpr : class Foo { private: template <typename T> struct Bar { constexpr Bar(){} constexpr int DoTheThing() const { return 1; } }; public: constexpr static auto b = Bar<int>{}; constexpr Foo() {} constexpr int DoTheThing() const { return b.DoTheThing(); } }; And I want to test that calling Foo::DoTheThing returns 1: int main() { constexpr Foo f; static_assert(f.DoTheThing() == 1, "DoTheThing() should return 1"); } GCC and Clang both complain here, but MSVC does not GCC says: error: constexpr Foo::Bar<T>::Bar()

Is it possible to know when is constexpr really a constexpr?

南楼画角 提交于 2019-11-29 03:07:10
Since the extended versions of constexpr (I think from C++14) you can declare constexpr functions that could be used as "real" constexpr, that is, the code executed at compile time or can behave as inline functions. So when can have this program: #include <iostream> constexpr int foo(const int s) { return s + 4; } int main() { std::cout << foo(3) << std::endl; const int bar = 3; std::cout << foo(bar) << std::endl; constexpr int a = 3; std::cout << foo(a) << std::endl; return 0; } Of course, the result is: 7 7 7 so far so good. So my question is: is there a way (possibly standard) to know in

template instantiation with constexpr function failure

最后都变了- 提交于 2019-11-29 01:22:02
I have template class C that has a non-type but reference template parameter to a type P : class P { public: int x; int y; }; template <const P &x> class C { public: const int &f() { return x.x; } }; I declared a global variable of type P : P p = {33,44}; I also declared a function that returns a reference to p : constexpr const P &h() { return p; } And then tried to use these in the following : C<p> o; // line 1 C<h()> oo; // line 2 Of course I have no problem with the first instantiation but the second. My compiler complains: error: non-type template argument does not refer to any

Static constexpr int vs old-fashioned enum: when and why?

三世轮回 提交于 2019-11-29 01:09:53
This is maybe a basic question, but I cannot see the response by myself right now. Consider the following code: template<bool b> struct T { static constexpr int value = (b ? 42 : 0); }; template<bool b> struct U { enum { value = (b ? 42 : 0) }; }; int main() { static_assert(T<true>::value == 42, "!"); static_assert(T<false>::value == 0, "!"); static_assert(U<true>::value == 42, "!"); static_assert(U<false>::value == 0, "!"); } I'm used to using structs like T , but more than once I've seen structs like U used for the same purpose (mostly traits definition). As far as I can see, they are both

constexpr to concatenate two or more char strings

不想你离开。 提交于 2019-11-28 23:40:31
I'm trying to make a constexpr function that will concatenate an arbitrary number of char arrays by working from the following answer by Xeo, which concatenates two char arrays. https://stackoverflow.com/a/13294458/1128289 #include <array> template<unsigned... Is> struct seq{}; template<unsigned N, unsigned... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<unsigned... Is> struct gen_seq<0, Is...> : seq<Is...>{}; template<unsigned N1, unsigned... I1, unsigned N2, unsigned... I2> constexpr std::array<char const, N1+N2-1> concat(char const (&a1)[N1], char const (&a2)[N2], seq<I1...>,

How can the compile-time be (exponentially) faster than run-time?

好久不见. 提交于 2019-11-28 23:35:42
问题 The below code calculates Fibonacci numbers by an exponentially slow algorithm: #include <cstdlib> #include <iostream> #define DEBUG(var) { std::cout << #var << ": " << (var) << std::endl; } constexpr auto fib(const size_t n) -> long long { return n < 2 ? 1: fib(n - 1) + fib(n - 2); } int main(int argc, char *argv[]) { const long long fib91 = fib(91); DEBUG( fib91 ); DEBUG( fib(45) ); return EXIT_SUCCESS; } And I am calculating the 45th Fibonacci number at run-time, and the 91st one at

Why is C++11 constexpr so restrictive?

耗尽温柔 提交于 2019-11-28 22:17:47
As you probably know, C++11 introduces the constexpr keyword. C++11 introduced the keyword constexpr, which allows the user to guarantee that a function or object constructor is a compile-time constant. [...] This allows the compiler to understand, and verify, that [function name] is a compile-time constant. My question is why are there such strict restrictions on form of the functions that can be declared. I understand desire to guarantee that function is pure, but consider this: The use of constexpr on a function imposes some limitations on what that function can do. First, the function must

constexpr initialization of array to sort contents

可紊 提交于 2019-11-28 21:54:36
This is a bit of a puzzle rather than a real-world problem, but I've gotten into a situation where I want to be able to write something that behaves exactly like template<int N> struct SortMyElements { int data[N]; template<typename... TT> SortMyElements(TT... tt) : data{ tt... } { std::sort(data, data+N); } }; int main() { SortMyElements<5> se(1,4,2,5,3); int se_reference[5] = {1,2,3,4,5}; assert(memcmp(se.data, se_reference, sizeof se.data) == 0); } except that I want the SortMyElements constructor to be constexpr . Obviously this is possible for fixed N ; for example, I can specialize

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

我们两清 提交于 2019-11-28 21:42:57
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 macros? GCC is right. However, there is a relatively simple workaround: #include "assert.h" inline void