constexpr

Why type const double is not captured by lambda from reaching-scope, but const int is?

帅比萌擦擦* 提交于 2019-12-17 19:27:36
问题 I seem can't understand why the following code with type const int compiles: int main() { using T = int; const T x = 1; auto lam = [] (T p) { return x+p; }; } $ clang++ -c lambda1.cpp -std=c++11 $ while this one with type const double doesn't: int main() { using T = double; const T x = 1.0; auto lam = [] (T p) { return x+p; }; } $ clang++ -c lambda2.cpp -std=c++11 lambda1.cpp:5:32: error: variable 'x' cannot be implicitly captured in a lambda with no capture-default specified auto lam = [] (T

`static constexpr` function called in a constant expression is…an error?

偶尔善良 提交于 2019-12-17 19:08:39
问题 I have the following code: class MyClass { static constexpr bool foo() { return true; } void bar() noexcept(foo()) { } }; I would expect that since foo() is a static constexpr function, and since it's defined before bar is declared, this would be perfectly acceptable. However, g++ gives me the following error: error: ‘static constexpr bool MyClass::foo()’ called in a constant expression This is...less than helpful, since the ability to call a function in a constant expression is the entire

Static constexpr int vs old-fashioned enum: when and why?

怎甘沉沦 提交于 2019-12-17 16:12:27
问题 This is maybe a basic question, but I cannot see the response by myself right now. Consider the following code: template<bool b> struct T { static constexpr int value = (b ? 42 : 0); }; template<bool b> struct U { enum { value = (b ? 42 : 0) }; }; int main() { static_assert(T<true>::value == 42, "!"); static_assert(T<false>::value == 0, "!"); static_assert(U<true>::value == 42, "!"); static_assert(U<false>::value == 0, "!"); } I'm used to using structs like T , but more than once I've seen

Can I obtain C++ type names in a constexpr way?

醉酒当歌 提交于 2019-12-17 15:53:47
问题 I would like to use the name of a type at compile time. For example, suppose I've written: constexpr size_t my_strlen(const char* s) { const char* cp = s; while(*cp != '\0') { cp++; }; return cp - s; } and now I want to have: template <typename T> constexpr auto type_name_length = my_strlen(typeid(T).name()); But alas, typeid(T).name() is just const char* , not constexpr... is there some other, constexpr way to get a type's name? 回答1: Well, you could, sort of, but probably not quite portable:

Passing constexpr objects around

人盡茶涼 提交于 2019-12-17 10:01:27
问题 I decided to give then new C++14 definition of constexpr a spin and to get the most out of it I decided to write a little compile-time string parser. However, I'm struggling with keeping my object a constexpr while passing it to a function. Consider the following code: #include <cstddef> #include <stdexcept> class str_const { const char * const p_; const std::size_t sz_; public: template <std::size_t N> constexpr str_const( const char( & a )[ N ] ) : p_( a ), sz_( N - 1 ) {} constexpr char

constexpr not working if the function is declared inside class scope

随声附和 提交于 2019-12-17 09:56:31
问题 I am using g++4.8.0, which doesn't contain earlier constexpr bug. Thus below code works fine: constexpr int size() { return 5; } int array[size()]; int main () {} However, if I enclose both the variable inside a class as static , then it gives compiler error: struct X { constexpr static int size() { return 5; } static const int array[size()]; }; int main () {} Here is the error: error: size of array ‘array’ is not an integral constant-expression Is it forbidden to use constexpr in such a way

how to initialize a constexpr reference

你说的曾经没有我的故事 提交于 2019-12-17 09:53:13
问题 I am trying to initialize a constexpr reference with no success. I tried #include <iostream> constexpr int& f(int& x) // can define functions returning constexpr references { return x; } int main() { constexpr int x{20}; constexpr const int& z = x; // error here } but I'm getting a compile time error error: constexpr variable 'z' must be initialized by a constant expression Dropping the const results in error: binding of reference to type 'int' to a value of type 'const int' drops qualifiers

how to initialize a constexpr reference

谁说我不能喝 提交于 2019-12-17 09:53:09
问题 I am trying to initialize a constexpr reference with no success. I tried #include <iostream> constexpr int& f(int& x) // can define functions returning constexpr references { return x; } int main() { constexpr int x{20}; constexpr const int& z = x; // error here } but I'm getting a compile time error error: constexpr variable 'z' must be initialized by a constant expression Dropping the const results in error: binding of reference to type 'int' to a value of type 'const int' drops qualifiers

Constexpr pointer value

梦想的初衷 提交于 2019-12-17 09:35:38
问题 I am trying to declare a constexpr pointer initialized to some constant integer value, but clang is foiling all my attempts: Attempt 1: constexpr int* x = reinterpret_cast<int*>(0xFF); test.cpp:1:20: note: reinterpret_cast is not allowed in a constant expression Attempt 2: constexpr int* x = (int*)0xFF; test.cpp:1:20: note: cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression Attempt 3: constexpr int* x = (int*)0 + 0xFF; test.cpp:1:28: note: cannot

constexpr not compiling in VC2013

我是研究僧i 提交于 2019-12-17 07:25:37
问题 This constexpr code does not compiled in Visual Studio 2013 version 12.0.21005.1 REL Is there a newer Visual Studio compiler that works with constexpr? #include <iostream> constexpr int factorial(int n) { return n <= 1 ? 1 : (n * factorial(n - 1)); } int main(void) { const int fact_three = factorial(3); std::cout << fact_three << std::endl; return 0; } output from compilation: 1>------ Build started: Project: Project1, Configuration: Debug Win32 ------ 1> Source.cpp 1>....\source.cpp(3):