Why does MSVC++11 rejects constexpr qualification of a function?
Quite simply – because Visual Studio doesn’t support constexpr (prior to Visual Studio 2015). Note that MSVC++11 is Visual Studio 2012; VC++10 is Visual Studio 2010.
Quite simply – because Visual Studio doesn’t support constexpr (prior to Visual Studio 2015). Note that MSVC++11 is Visual Studio 2012; VC++10 is Visual Studio 2010.
Here’s another solution, which is more generic (applicable to any expression, without defining a separate template each time). This solution leverages that (1) lambda expressions can be constexpr as of C++17 (2) the type of a captureless lambda is default constructible as of C++20. The idea is, the overload that returns true is selected when … Read more
This answer is no longer correct as of C++20. No. From [dcl.constexpr]/3 (7.1.5, “The constexpr specifier”): The definition of a constexpr function shall satisfy the following requirements: — it shall not be virtual
Have you ever watch Sprout’s implementaion? Sprout is header-only library that provide C++11/14 constexpr based Containers, Algorithms, Random numbers, Parsing, Ray tracing, Synthesizer, and others. https://github.com/bolero-MURAKAMI/Sprout/tree/master/sprout/math
As Luc Danton notes, your attempts are blocked by the rules in [expr.const]/2 which say that various expressions are not allowed in core constant expressions, including: — a reinterpret_cast — an operation that would have undefined behavior [Note: including […] certain pointer arithmetic […] — end note] The first bullet rules out your first example. … Read more
As per the draft basic.types#10 possibly cv-qualified class type that has all of the following properties: A possibly cv-qualified class type that has all of the following properties: (10.5.1) – it has a trivial destructor, (10.5.2) – it is either a closure type, an aggregate type, or has at least one constexpr constructor or constructor … Read more
After further investigation, it turns out there exists a minor modification that can be performed to the next() function, which makes the code work properly on clang++ versions above 7.0.0, but makes it stop working for all other clang++ versions. Have a look at the following code, taken from my previous solution. template <int N> … Read more
As T.C. demonstrated with some links in a comment, the standard is not quite clear on this; a similar problem arises with trailing return types using decltype(memberfunction()). The central problem is that class members are generally not considered to be declared until after the class in which they’re declared is complete. Thus, regardless of the … Read more
Question: why aren’t decomposition declarations be allowed to be constexpr? (apart from “because the Standard says so”). There is no other reason. The standard says in [dcl.dcl] p8: The decl-specifier-seq shall contain only the type-specifier auto (7.1.7.4) and cv-qualifiers. That means it can’t be declared with constexpr. This was the subject of a National Body … Read more
This is easy to explain through an example. Consider struct Cat { void meow() { } }; struct Dog { void bark() { } }; and template <typename T> void pet(T x) { if(std::is_same<T, Cat>{}){ x.meow(); } else if(std::is_same<T, Dog>{}){ x.bark(); } } Invoking pet(Cat{}); pet(Dog{}); will trigger a compilation error (wandbox example), because both … Read more