constexpr

Why can function pointers be `constexpr`?

不打扰是莪最后的温柔 提交于 2020-06-24 07:06:22
问题 How does the compiler know where in memory the square root will be before the program is executed? I thought the address would be different everytime the program is executed, but this works: constexpr double(*fp)(double) = &sqrt; cout << fp(5.0); Is it because the address is relative to another address in memory? I don't think so because the value of fp is large: 0x720E1B94. 回答1: How does the compiler know where in memory the square root will be before the program is executed? The tool chain

Is there a non-indirection, non-hack way to guarantee that a constexpr function only be callable at compile time?

我们两清 提交于 2020-06-21 19:08:12
问题 At the moment, we have two primary options for compile-time evaluation: template metaprogramming (generally using template structs and/or variables), and constexpr operations 1 . template<int l, int r> struct sum_ { enum { value = l + r }; }; // With struct. template<int l, int r> const int sum = sum_<l, r>::value; // With struct & var. template<int l, int r> const int sub = l - r; // With var. constexpr int mul(int l, int r) { return l * r; } // With constexpr. Of these, we are guaranteed

constexpr version of ::std::function

孤街浪徒 提交于 2020-06-16 02:50:11
问题 I am in search of a ::std::function usable in constexpr. Use case: I have a function which takes a function pointer as an argument, and a second which passes a lambda to the first function. Both are fully executable at compile time, so I want to constexpr them. Eg: template <class _Type> class ConstexprFunctionPtr { private: using Type = typename ::std::decay<_Type>::type; const Type function; public: constexpr inline ConstexprFunctionPtr(const Type f) : function(f) { } template <typename...

What does [decl.constexpr].5 mean exactly?

谁说胖子不能爱 提交于 2020-06-16 02:26:30
问题 The standard on constexpr functions states under point 5 of [decl.constexpr]: For a non-template, non-defaulted constexpr function or a non-template, non-defaulted, non-inheriting constexpr constructor, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression (5.19), the program is ill-formed; no diagnostic required. It goes on to give the following example for this: constexpr int f(bool b){ return b ?

constexpr struct member initialisation

一曲冷凌霜 提交于 2020-06-12 04:33:26
问题 This code compiles: struct Info { constexpr Info(bool val) : counted(false), value(unsigned(val)) {} constexpr Info(unsigned val) : counted(true), value(val) {} bool counted; unsigned value; }; constexpr const auto data = std::array{ Info{true}, Info{42u} }; struct Foo { constexpr static inline const auto data = std::array{ Info{true}, Info{42u} }; }; This code does not: struct Foo { struct Info { constexpr Info(bool val) : counted(false), value(unsigned(val)) {} constexpr Info(unsigned val)

Why is `constexpr` part of the C++14 template prototype for `std::max()`?

久未见 提交于 2020-05-28 07:55:49
问题 Per cplusplus.com, here, the default C++11 prototype for std::max() is: template <class T> const T& max(const T& a, const T& b); In the C++14 version, however constexpr was added: template <class T> constexpr const T& max(const T& a, const T& b); Why is constexpr here and what does it add? Note on possible duplicate I think my question is not a duplicate of this one (Difference between `constexpr` and `const`), because I am asking about a very specific usage of constexpr , whereas that

A constexpr function with delayed initialization of local variables

我的未来我决定 提交于 2020-05-14 14:56:46
问题 I am trying to write a constexpr function of the form: constexpr int foo(bool cond) { int a, b, c; if (cond) { a = 1; b = 2; c = 3; } else { a = -1; b = -2; c = -3; } return a + b + c; } However, the compiler complains that I am using uninitialized variables, despite the fact that the eventual initialization of the local variables is guaranteed. I could re-write the function to use ternary operators, that is, int a = cond ? 1 : -1; , etc., but I would prefer not to. Is there a way to convince

Constexpr Class taking const references not compiling

旧街凉风 提交于 2020-05-07 07:05:16
问题 I have the following sample code template<class T1, class T2> class Operation { public: constexpr Operation(const T1& lhs, const T2& rhs) noexcept : m_lhs(lhs), m_rhs(rhs) { } private: const T1& m_lhs; const T2& m_rhs; }; int main() { constexpr int a = 3; constexpr int b = 4; constexpr Operation op(a, b); return 0; } Compiling this with cygwin (gcc 8.2) I get error: 'Operation<int, int>{a, b}' is not a constant expression: constexpr Operation op(a, b); With MSVC 2019 it compiles fine, but

Can I use a constexpr value in a lambda without capturing it?

半城伤御伤魂 提交于 2020-04-29 07:19:01
问题 I would want to use a constexpr value in a lambda. Reading the answer to Using lambda captured constexpr value as an array dimension , I assumed the following should work: #include<array> int main() { constexpr int i = 0; auto f = []{ std::array<int, i> a; }; return 0; } However, Clang 3.8 (with std=c++14) complains that variable 'i' cannot be implicitly captured in a lambda with no capture-default specified Should this be considered a bug in clang 3.8? BTW: The above code does compile with

Invoking `constexpr` member function through reference - clang vs gcc

梦想的初衷 提交于 2020-03-18 05:03:09
问题 Consider the following example ( snippet (0) ): struct X { constexpr int get() const { return 0; } }; void foo(const X& x) { constexpr int i = x.get(); } int main() { foo(X{}); } The above example compiles with all versions of g++ prior to g++ 10.x , and never compiled under clang++ . The error message is: error: 'x' is not a constant expression 8 | constexpr int i = x.get(); | live example on godbolt.org The error kind of makes sense, as x is never a constant expression in the body of foo ,