constexpr

are constexpr functions that load files possible in C++?

你说的曾经没有我的故事 提交于 2019-12-09 16:34:10
问题 There was a similar question here but it had no valuable information so I want to ask this again - is it possible to load contents of an arbitrary file using a constexpr function? I know that this seems impossible since all the possible functions that allow file I/O ( fopen , open ...) are not constexpr so cannot be called in this scenario. However - since there are many people here who follow the development of c++17 and on - is there any hope that further standards will include some file I

simulate compile time reflection in C++

夙愿已清 提交于 2019-12-09 10:15:56
问题 I have a following struct: struct Data { std::string firstMember; std::string secondMember; std::string thirdMember; }; I want to select one of the members by string name in constexpr manner, like Data instance; auto& member = getMember(instance, "firstMember"); getMember is constexpr function/struct/macros/whatever in question and expression should be (I want it to be) optimized into simple auto& member = instance.firstMember; . My desire here is to be able to call getMember from another

As far as I can tell the function below is not constexpr, but the code compiles in clang and g++. What am I missing?

狂风中的少年 提交于 2019-12-09 09:25:06
问题 I got this example from §5.19/2 in N4140: constexpr int incr(int &n) { return ++n; } As far as I can tell, this is not a constexpr function. But the snippet compiles in clang and g++. See live example. What am I missing here? 回答1: In C++14 the rules for constexpr function were relaxed and the paper N3597: Relaxing constraints on constexpr functions. The paper goes into the rationale and the effects and it includes the following ( emphasis mine ): As in C++11, the constexpr keyword is used to

The body of constexpr function not a return-statement

不想你离开。 提交于 2019-12-09 05:02:19
问题 In the following program, I have added an explicit return statement in func() , but the compiler gives me the following error: m.cpp: In function ‘constexpr int func(int)’: m.cpp:11:1: error: body of constexpr function ‘constexpr int func(int)’ not a return-statement } This is the code: #include <iostream> using namespace std; constexpr int func (int x); constexpr int func (int x) { if (x<0) x = -x; return x; // An explicit return statement } int main() { int ret = func(10); cout<<ret<<endl;

Template tricks with const char* as a non-type parameter

你离开我真会死。 提交于 2019-12-08 22:50:59
问题 I am very well aware that passing directly a const char* as a template non-type parameter is erroneous, since two identical string literals defined in two different translation units may have different addresses (although most of the time the compilers use the same address). There is a trick one may use, see code below: #include <iostream> template<const char* msg> void display() { std::cout << msg << std::endl; } // need to have external linkage // so that there are no multiple definitions

Where in C++14 Standard does it say that a non-constexpr function cannot be used in a definition of a constexpr function?

邮差的信 提交于 2019-12-08 19:28:57
问题 For example the code below doesn't compile unless incr() is declared constexpr : int incr(int& n) { return ++n; } constexpr int foo() { int n = 0; incr(n); return n; } Looking at §7.1.5/3 in C++14 we have: The definition of a constexpr function shall satisfy the following constraints: (3.1) — it shall not be virtual (10.3); (3.2) — its return type shall be a literal type; (3.3) — each of its parameter types shall be a literal type; (3.4) — its function-body shall be = delete, = default, or a

Constexpr tricks

匆匆过客 提交于 2019-12-08 19:18:24
问题 I think it's not possible but I'd like to ask you before give up about it. I want something like a constexpr increment. #include <iostream> constexpr int inc() { static int inc = 0; return inc++; } class Foo { static const int Type = inc(); }; class Foo2 { static const int Type = inc(); }; int main() { std::cout << "Foo1 " << Foo1::Type << st::endl; std::cout << "Foo2 " << Foo2::Type << st::endl; return 0; } I want to call it into some classes not manually (I use CRTP for that), to give a

Is there a way to forward argument to inner constexpr function?

痴心易碎 提交于 2019-12-08 16:35:25
问题 The question: is it possible to evaluate constant expression inside a function by passing (maybe with some kind of "perfect forwarding") its argument to inner constexpr function? Example: constexpr size_t foo(char const* string_literal) { return /*some valid recursive black magic*/; } void bar(char const* string_literal) { // works fine constexpr auto a = foo("Definitely string literal."); // compile error: "string_literal" is not a constant expression constexpr auto b = foo(string_literal);

Should I always replace 'const int' with 'constexpr int' in C++11 whenever possible?

随声附和 提交于 2019-12-08 16:33:35
问题 Would you replace const int one = 1; const int two = 2; with this? constexpr int one = 1; constexpr int two = 2; Is my understanding correct that both blocks are semantically identical and that it is currently merely a matter of taste? On the other hand, as constexpr implies const , you could argue that it is more consistent to always prefer the more restrictive form, even in trivial situations where it does not make a difference? (I understand that the situation completely changes when the

Is an empty class usable as a constexpr variable without an initializer or explicit default constructor?

三世轮回 提交于 2019-12-08 16:01:02
问题 Given the following code: struct f { }; int main(){ constexpr f f1 ; //const f f1 ; // This also has the same issue //constexpr f f1 = {} ; //This works } clang and gcc disagree over whether it is valid, with clang providing the following diagnostic ( see it live ): error: default initialization of an object of const type 'const f' without a user-provided default constructor constexpr f f1 ; ^ {} As far as I can tell f is a literal type and it is initialized by the implicitly defaulted