constexpr

constexpr member functions that don't use this?

◇◆丶佛笑我妖孽 提交于 2019-12-08 15:59:12
问题 Please consider the following two C++14 programs: Program 1: struct S { constexpr int f() const { return 42; } }; S s; int main() { constexpr int x = s.f(); return x; } Program 2: struct S { constexpr int f() const { return 42; } }; int g(S s) { constexpr int x = s.f(); return x; } int main() { S s; return g(s); } Are neither, either or both of these programs ill-formed? Why/why not? 回答1: Both programs are well-formed. The C++14 standard requires that s.f() be a constant expression because it

How to declare constexpr C string?

烂漫一生 提交于 2019-12-08 15:54:07
问题 I think i quite understand how to use the keyword constexpr for simple variable types, but i'm confused when it comes to pointers to values. I would like to declare a constexpr C string literal, which will behave like #define my_str "hello" That means the compiler inserts the C string literal into every place where i enter this symbol, and i will be able to get its length at compile-time with sizeof. Is it constexpr char * const my_str = "hello"; or const char * constexpr my_str = "hello"; or

If structured bindings cannot be constexpr why can they be used in constexpr function?

我们两清 提交于 2019-12-08 15:13:20
问题 According to this answer apparently there is no good reason why structured bindings are not allowed to be constexpr, yet the standard still forbids it. In this case, however, shouldn't the use of the structured bindings inside the constexpr function also be prohibited? Consider a simple snippet: #include <utility> constexpr int foo(std::pair<int, int> p) { auto [a, b] = p; return a; } int main() { constexpr int a = foo({1, 2}); static_assert(a == 1); } Both gcc and clang does not cause

Why constexpr works for impure functions

元气小坏坏 提交于 2019-12-08 15:07:37
问题 while browsing one of my old questions on constexpr I stumbled onto a very(IMHO) important comment. Basically it boils down to : (this is legal C++11 :( ) constexpr double f(bool b) { return b? 42:42/(rand()+1); // how pure is rand ;) } My question is what is the reason this is allowed by the standard. Since Im a big fan of referential transparency I hope they have a good reason :) and I would like to know it. BTW there is related Q but most of the A even dont mention pure thing, or when they

GCC constexpr lambdas in constexpr functions and evaluation in compile time

混江龙づ霸主 提交于 2019-12-08 12:13:24
问题 Code first, we have the following piece of code that is used to accumulate a constexpr std::array in compile time: template <typename T, std::size_t N, typename O> constexpr T compile_time_accumulator(const std::array<T, N> const &A, const std::size_t i, const O& op, const T initialValue) { return (i < N) ? op(A[i], compile_time_accumulator(A, i + 1, op, initialValue)) : initialValue; } and the following code example to test/varify it (i.e., that it evaluates in compile time): constexpr std:

Address Constant Expressions

夙愿已清 提交于 2019-12-08 10:56:26
I'm delving into address constant expressions while reading "C++ Programming Language 4th edition" book. It has a short paragraph which describes address constant expressions: The address of a statically allocated object, such as a global variable, is a constant. However, its value is assigned by the linker, rather than the compiler, so the compiler cannot know the value of such an address constant. That limits the range of constant expressions of pointer and reference type. For example: constexpr const char* p1 = "asdf"; constexpr const char* p2 = p1; //OK constexpr const char* p2 = p1+2; /

Take the length of a string implicitly in templates

故事扮演 提交于 2019-12-08 06:09:36
问题 Ok, suppose I have this magic template that takes a char array and turns it into a template parameter list. For example: "thing" is translated to list<'t','h','i','n','g'> : template<const char*, size_t> ArrayToList; You use it like this: constexpr char string[] = "this is a test"; typedef ArrayToList<string,14> list; Is there any way to somehow detect the string length implicitly so that all I have to do is: typedef ArrayToList<string> list; 回答1: Unfortunately, it can't be done: You would

ODR of template class with static constexpr member

本小妞迷上赌 提交于 2019-12-08 05:38:11
问题 I know, there are many answered question about linkage of a static (constexpr) members. But I wonder, why using a template class out-of-line definition works in a header file but not for a specialized class. a) This works without linker error: template<typename, typename> struct Foobar; template<typename T> struct Foobar<int, T> { static constexpr std::array<int, 1> a = {{1}}; }; template<typename T> constexpr std::array<int, 1> Foobar<int, T>::a; // foo.cpp std::cout << Foobar<int, int>::a[0

Address Constant Expressions

心不动则不痛 提交于 2019-12-08 05:22:50
问题 I'm delving into address constant expressions while reading "C++ Programming Language 4th edition" book. It has a short paragraph which describes address constant expressions: The address of a statically allocated object, such as a global variable, is a constant. However, its value is assigned by the linker, rather than the compiler, so the compiler cannot know the value of such an address constant. That limits the range of constant expressions of pointer and reference type. For example:

Conflicting declaration using constexpr and auto in C++11

戏子无情 提交于 2019-12-08 05:06:46
问题 I'm experimenting with C++11, constexpr and auto. I'm failing to understand why this code does not compile: template<class T, T t> struct TestEle2 { }; template<class T, T t> struct DStruct {int a;}; template<char t> struct TestEle2<char, t> { static constexpr auto f = 5; static constexpr auto g = &DStruct<char, t>::a; }; template<char c> constexpr decltype(TestEle2<char, c>::f) TestEle2<char, c>::f ; // This compiles template<char c> constexpr decltype(TestEle2<char, c>::g) TestEle2<char, c>