constexpr

constexpr non-static member function with non-constexpr constructor (gcc,clang differ)

一笑奈何 提交于 2020-01-03 08:09:07
问题 For this code: struct S { S(int m): m(m) {} constexpr int f() const { return m; } int m; }; int main() { S s(1); } it is compiled with no warnings or errors by clang 3.6, 3.7 and 3.8 with -std=c++14 . But in g++ 5.x the following errors occur: main.cpp:4:19: error: enclosing class of constexpr non-static member function 'int S::f() const' is not a literal type constexpr int f() const { return m; } ^ main.cpp:1:8: note: 'S' is not literal because: struct S ^ main.cpp:1:8: note: 'S' is not an

constexpr in C++11 and C++14 (not a difference to const keyword) [duplicate]

安稳与你 提交于 2020-01-02 07:09:56
问题 This question already has answers here : Difference between `constexpr` and `const` (8 answers) Closed 2 years ago . In the "Exploring C++17 and beyond" presentation by Mike Isaacson at one point (https://youtu.be/-ctgSbEfRxU?t=2907) there is question about writing: const constexpr .... vs single const. Mike said that in C++11 constexpr implies const and in C++14 it does not. Is it true? I've tried to find prove for that but I couldn't. I'm not asking about difference between const and

What is the difference between a static const and constexpr variable?

£可爱£侵袭症+ 提交于 2020-01-02 03:24:07
问题 I understand that a constexpr variable can be used at compiletime. For a template, or static asser for instance. But if I want to do that without constexpr I can with static const . What is since C++11/14 introduced constexpr the difference between constexpr int a = 3; //AND static const int a = 3; Thank you! Another way to see this question is which should I use? 回答1: The main difference that I know is, the value of constexpr must be known in compile-time while a const static can be assigned

C++1y/C++14: Assignment to object outside its lifetime is not allowed in a constant expression?

你离开我真会死。 提交于 2020-01-02 00:34:30
问题 Is the following C++14/C++1y program ill-formed according to the current draft? #include <cstddef> template<typename T, size_t n> struct literal_array { T data[n]; }; template<typename T, size_t n, size_t m> constexpr literal_array<T, n+m> operator+(literal_array<T, n> a, literal_array<T, m> b) { literal_array<T, n+m> x; for (size_t i = 0; i < n; i++) x.data[i] = a.data[i]; for (size_t i = 0; i < m; i++) x.data[n+i] = b.data[i]; return x; } int main() { constexpr literal_array<int, 3> a = { 1

Constexpr find for array using c++17

ⅰ亾dé卋堺 提交于 2020-01-01 15:06:53
问题 I am trying to write a constexpr find function that will return the index of a std::array containing a certain value. The function below seems to work OK except when the contained type is const char* : #include <array> constexpr auto name1() { return "name1"; } constexpr auto name2() { return "name2"; } template <class X, class V> constexpr auto find(X& x, V key) { std::size_t i = 0; while(i < x.size()) { if(x[i] == key) return i; ++i; } return i; } int main() { constexpr std::array<const

In C++14 can a constexpr member change a data member?

☆樱花仙子☆ 提交于 2020-01-01 12:05:45
问题 In C++14, since constexpr are not implicitly const anymore, can a constexpr member function modify a data member of a class: struct myclass { int member; constexpr myclass(int input): member(input) {} constexpr void f() {member = 42;} // Is it allowed? }; 回答1: Yes they are, I believe this change started with proposal N3598: constexpr member functions and implicit const and eventually became part of N3652: Relaxing constraints on constexpr functions which changed section 7.1.5 paragraph 3 what

Initializing constexpr with const: Different treatment for int and double

放肆的年华 提交于 2020-01-01 04:11:07
问题 The following code fails to compile live on Ideone: #include <iostream> using namespace std; int main() { const double kPi = 3.14; constexpr double kPi2 = 2.0*kPi; cout << kPi2; } The error message is: prog.cpp: In function 'int main()': prog.cpp:6:30: error: the value of 'kPi' is not usable in a constant expression constexpr double kPi2 = 2.0*kPi; ^ prog.cpp:5:15: note: 'kPi' was not declared 'constexpr' const double kPi = 3.14; Substituting the const declaration for kPi with constexpr , it

How can I get the depth of a multidimensional std::vector at compile time?

こ雲淡風輕ζ 提交于 2020-01-01 01:11:11
问题 I have a function that takes a multidimensional std::vector and requires the depth (or the number of dimensions) to be passed in as a template parameter. Instead of hardcoding this value I would like to write a constexpr function that will take the std::vector and return the depth as an unsigned integer value. For example: std::vector<std::vector<std::vector<int>>> v = { { { 0, 1}, { 2, 3 } }, { { 4, 5}, { 6, 7 } }, }; // Returns 3 size_t depth = GetDepth(v); This needs to be done at compile

Does specifying constexpr on constructor automatically makes all objects created from it to be constexpr?

冷暖自知 提交于 2019-12-31 09:08:07
问题 Here is my code: class test{ public: constexpr test(){ } constexpr int operator+(const test& rhs){ return 1; } }; int main(){ test t; //constexpr word isn't necessary constexpr int b = t+test(); // works at compile time! int w = 10; // ERROR constexpr required constexpr int c = w + 2; // Requires w to be constexpr return 0; } I notice that it worked even though I didn't specify test to be constexpr . I tried replicating the result by doing the same with int but i get errors. Specifically, it

Is it legal to use side-effects in exceptions thrown by constexpr?

心不动则不痛 提交于 2019-12-30 07:58:10
问题 Normally, constexpr must be free of side-effects. However, I just discovered that it is possible to use side-effects in the constructors of thrown exceptions. That technique can be used to emulate assert() for constexpr functions, as it is demonstrated in the following program. #include <iostream> #include <cstdlib> #include <stdexcept> struct constexpr_precond_violated : std::logic_error { constexpr_precond_violated(const char* msg) : std::logic_error(msg) { std::cerr << msg << '\n'; abort()