constexpr

constexpr static member before/after C++17

邮差的信 提交于 2019-11-27 08:54:40
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 Class<i>::J; Now I read cppreference: Definitions and ODR and cppreference: static members , which state,

Why isn't a for-loop a compile-time expression?

回眸只為那壹抹淺笑 提交于 2019-11-27 08:54:10
If I want to do something like iterate over a tuple, I have to resort to crazy template metaprogramming and template helper specializations. For example, the following program won't work: #include <iostream> #include <tuple> #include <utility> constexpr auto multiple_return_values() { return std::make_tuple(3, 3.14, "pi"); } template <typename T> constexpr void foo(T t) { for (auto i = 0u; i < std::tuple_size<T>::value; ++i) { std::get<i>(t); } } int main() { constexpr auto ret = multiple_return_values(); foo(ret); } Because i can't be const or we wouldn't be able to implement it. But for

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

最后都变了- 提交于 2019-11-27 08:48:55
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 (condition) { return 0; } else { // optimized out even without "constexpr if" return 1; } } Godbolt

static constexpr member of same type as class being defined

一曲冷凌霜 提交于 2019-11-27 07:46:45
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 Foo() {} static const Foo f; }; constexpr Foo Foo::f = Foo(); Now g++ complains about a

Lambda capturing constexpr object

♀尐吖头ヾ 提交于 2019-11-27 07:35:04
问题 GCC 4.7.2 compiles this: constexpr int i = 5; []{ std::integral_constant< int, i >(); }; // nonstandard: i not captured but not this: constexpr int i = 5; [&i]{ std::integral_constant< int, i >(); }; // GCC says i not constexpr The latter example appears correct to me, according to C++11 §5.1.2/15: An entity is captured by reference if it is implicitly or explicitly captured but not captured by copy. It is unspecified whether additional unnamed non-static data members are declared in the

Relation between constexpr and pure functions

一曲冷凌霜 提交于 2019-11-27 07:34:56
问题 Am I right, that: Any function defined with constexpr is a pure function, and Any pure function can be and must be defined with constexpr if it's not very expensive for compiler. And if so, why arent <cmath> 's functions defined with constexpr ? 回答1: To add to what others have said, consider the following constexpr function template: template <typename T> constexpr T add(T x, T y) { return x + y; } This constexpr function template is usable in a constant expression in some cases (e.g., where

Evaluating strlen at compilation time?

◇◆丶佛笑我妖孽 提交于 2019-11-27 07:05:55
问题 If my code has this constexpr string constexpr char my_str[] = "hello"; the type of my_str contains information about its size, i.e. sizeof(my_str) is a constant 6, and can be used anywhere a constant is required. What about strlen(my_str) ? Can/should it also be evaluated to a compile-time constant? Here is an example for yes: https://ideone.com/2U65bN Here is an example for no: http://coliru.stacked-crooked.com/a/8cb094776dfc5969 What does the Standard say about this? Surely not "maybe"?

Is it possible to use std::string in a constexpr?

六眼飞鱼酱① 提交于 2019-11-27 06:19:50
Using C++11, Ubuntu 14.04, GCC default toolchain . This code fails: constexpr std::string constString = "constString"; error: the type ‘const string {aka const std::basic_string}’ of constexpr variable ‘constString’ is not literal... because... ‘std::basic_string’ has a non-trivial destructor Is it possible to use std::string in a constexpr ? (apparently not...) If so, how? Is there an alternative way to use a character string in a constexpr ? tenfour No, and your compiler already gave you a comprehensive explanation. But you could do this: constexpr char constString[] = "constString"; At

When and why would you use static with constexpr?

人盡茶涼 提交于 2019-11-27 05:29:05
问题 As a disclaimer, I have done my research on this before asking. I found a similar SO question but the answer there feels a bit "strawman" and didn't really answer the question for me personally. I've also referred to my handy cppreference page but that doesn't offer a very "dumbed down" explanation of things most times. Basically I'm still ramping up on constexpr , but at the moment my understanding is that it requires expressions to be evaluated at compile time. Since they may only exist at

Static constexpr odr-used or not?

蓝咒 提交于 2019-11-27 05:19:46
How come that the following works on gcc but doesn't on clang , ( see it live ): constexpr int giveMeValue() { return 42; } struct TryMe { static constexpr int arr[1] = { giveMeValue() }; }; int main() { int val = TryMe::arr[0]; return val; } I get an unresolved external symbol with clang. Is TryMe::arr[0] an object? If it is, is it odr-used? TryMe::arr is odr-used but you don't provide a definition ( see it live ): constexpr int TryMe::arr[1]; Why is the result inconsistent between gcc and clang ? This is because odr violations do not require a disagnostic, from both the C++11 and C++14 draft