constexpr

Computing the factorial of a small integer at compile time

随声附和 提交于 2019-12-10 13:19:22
问题 I just implemented (once again) a recursive template for computing the factorial of an integer at compile time (who would had thought that some day I'll actually need it!). Still, instead of rolling my own, I went to Boost looking for an answer. However, the factorial function in special math specifically forbids its use with integer types, so I just wrote my own. Still, is there another function in Boost that I should use? Should I cast my integer to double and use the boost::factorial

GCC accepts `constexpr struct {} s;` but Clang rejects it. Who is correct?

雨燕双飞 提交于 2019-12-10 12:28:40
问题 The following code compiles fine with GCC: constexpr struct {} s; But Clang rejects it with the following error: error: default initialization of an object of const type 'const struct (anonymous struct at …)' without a user-provided default constructor I've tested all versions of GCC and Clang that I was able to find at https://gcc.godbolt.org/. Each version of GCC accepts the code and each version of Clang rejects it. I wonder which compiler is correct in this case? What does the standard

constexpr reference to non-const object

你。 提交于 2019-12-10 09:24:16
问题 Is it permitted to declare a non-const reference as constexpr ? Example code: int x = 1; constexpr int& r = x; This is accepted by gcc and clang (I tried several current and past versions of both, back to C++11, and all accepted it). However I think it should not be accepted because C++14 [dcl.constexpr/9] says: if a constexpr specifier is used in a reference declaration, every full- expression that appears in its initializer shall be a constant expression and x is not a constant expression.

Different behavior observed with constexpr auto/char-array variable

ε祈祈猫儿з 提交于 2019-12-10 09:08:06
问题 Following up with this question Having a constexpr static string gives a linker error In the question, this code wasn't able to compile: #include <iostream> struct Test { static constexpr char text[] = "Text"; }; int main() { std::cout << Test::text << std::endl; // error: undefined reference to `Test::text' } From the comment, this code is able to compile: #include <iostream> struct Test { static constexpr auto text = "Text"; }; int main() { std::cout << Test::text << std::endl; } My

Template non-type parameter deduction

泪湿孤枕 提交于 2019-12-10 05:16:55
问题 Is it possible to deduce template value (not type) for a c++17 function? The function foo: template<int I> int foo() { return (I); } Can be called via: foo<5>(); And will return 5. Template types can be deduced through the type of a function argument. Is it possible to do the same in some way for the template value? For example: template<int I = x> int bar(const int x) { return (I); } This obviously wont work (because for one x is required before its declaration), but might there be some C+

Is a compiler forced to reject invalid constexpr?

时光怂恿深爱的人放手 提交于 2019-12-10 02:38:02
问题 #include <exception> constexpr bool foo(bool x) { return x ? true : throw std::exception(); } int main() { // 1) must never be compiled // static_assert(foo(false), ""); // 2) must always be compiled? const bool x = foo(false); // 3) must never compile? constexpr bool y = foo(false); return 0; } I'm sure that (1) must lead to a compile error. I'm quite sure that (2) must not be rejected at compile time, though it will fail at runtime. The interesting case is the constexpr variable (3). In

Why does compiler allow out-of-bounds array access even with constexpr index?

五迷三道 提交于 2019-12-10 01:54:41
问题 For example if we have an std::array and we instantiate an element that is out of bound using constexpr the compiler wouldn't report error: constexpr int EvaluateSpecialArrayIndex(int a) { return a * sizeof(int); } array<int, 5> arr; cout << arr[98] << endl; //compiles fine cout << arr[EvaluateSpecialArrayIndex(4)] << endl; //the same as above Can't we restrict this somehow? 回答1: To ensure that constexpr functions are evaluated at compile time, you must force them to be by making their result

Why must a string be constructed at run-time? [duplicate]

喜欢而已 提交于 2019-12-10 01:07:22
问题 This question already has answers here : Is it possible to use std::string in a constexpr? (4 answers) Closed 4 years ago . Can C-Strings or std::string s be created as constexpr or must they be created at run-time? With gcc 4.9.2 I can do this: constexpr const char foo[] = "blee"; (Sadly the November 2013 Customer Technology Preview does not allow Visual Studio to support this: https://stackoverflow.com/a/29255013/2642059) But even with gcc 4.9.2 I cannot do this: constexpr const std::string

Why does MSVC++11 rejects constexpr qualification of a function?

橙三吉。 提交于 2019-12-10 00:35:04
问题 So, playing around with constexpr, MSVC (Visual Studio 2012) gave me an error while trying to qualify my function with the constexpr keyword using this simple program (includes omitted): 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; } constexpr was underlined red with the following message: Error : this declaration has no storage class or type specifier and

About ODR-violations and template variables

青春壹個敷衍的年華 提交于 2019-12-09 16:39:31
问题 I know that template functions don't suffer of multiple definitions when linking, like member functions defined inside a class, which are inline by default. Also, constexpr objects have internal linkage, but template variables have external linkage (I mean at namespace scope and for C++14 in both cases). What about? template<class T> constexpr T i_am_odr_safe{}; Does i_am_odr_safe have external or internal linkage in C++14? and is it safe regarding multiple-definitions like function templates