constexpr

Why Won't Visual Studio let me use a Templatized, constexpr Function in enable_if?

こ雲淡風輕ζ 提交于 2019-12-23 08:56:43
问题 So I've boiled this down to the minimal, complete, verifiable example and it seems that Visual Studio 2015 just won't allow me to use a templatized, constexpr function in an enable_if . For example: template <typename T> constexpr bool condition() { return sizeof(T) > 1; } Gives me the error: error C2995: enable_if<_Test,T>::type test(void) : function template has already been defined When I try to use it in substitution failure is not an error compilation like this: template <typename T>

implicit constexpr?

 ̄綄美尐妖づ 提交于 2019-12-23 08:13:32
问题 Can C++11 compilers (and do they) notice that a function is a constexpr and treat them as such even if they are not declared to be constexpr ? I was demonstrating the use of constexpr to someone using the example straight from the Wikipedia: int get_five() {return 5;} int some_value[get_five() + 7]; // Create an array of 12 integers. Ill-formed C++ To my surprise the compiler was OK with it. So, I further changed get_five( ) to take a few int parameters, multiply them and return the result

How can I do a runtime assert in a constexpr function?

穿精又带淫゛_ 提交于 2019-12-23 07:46:28
问题 From what I understand, a constexpr function can be executed at compile time as well as runtime, depending on if the entire evaluation can be done at compile time or not. However, you cannot overload this function to have a runtime and a compile time counterpart. So my question is, how can I put in a runtime assert to ensure that the execution of the runtime function is passed valid parameters along with my static_assert? 回答1: Eric Niebler covers this issue well in Assert and Constexpr in C+

Which should I prefer for a constant within a function: constexpr const or enum?

陌路散爱 提交于 2019-12-23 07:46:01
问题 I'm used to definition my constants with enum { my_const = 123; } , since in classes, using static constexpr requires some code outside of the class definition (see this question). But - what about in function bodies? Lately I've been noticing people just having constexpr variables in their functions (not even bothering to const them actually), and I was wondering whether I'm a fool who's behind the times with my int foo(int x) { enum : int { bar = 456 }; return x + bar; } So, my question is:

constexpr void function rejected

拜拜、爱过 提交于 2019-12-23 07:37:07
问题 I have this very simple function which won't compile. constexpr void func() { } The error I'm getting is: error: invalid return type ' void ' of constexpr function ' constexpr void func() ' constexpr void func() In C++14, void is a literal type [§3.9/10]: A type is a literal type if it is: void; or a scalar type; or a reference type; or an array of literal type; or a class type (Clause 9) that has all of the following properties: it has a trivial destructor, it is an aggregate type (8.5.1) or

constexpr array of constexpr objects using move ctor

泪湿孤枕 提交于 2019-12-23 07:31:09
问题 I have a class with a constexpr value constructor, but no copy or move ctor class C { public: constexpr C(int) { } C(const C&) = delete; C& operator=(const C&) = delete; }; int main() { constexpr C arr[] = {1, 2}; } I've found that this code doesn't work because it's actually trying to use the move constructor for C rather than the value constructor to construct in place. One issue is that I want this object to be unmovable (for test purposes) but I thought "okay, fine, I'll add a move

Forward declare a constexpr variable template

微笑、不失礼 提交于 2019-12-23 07:21:41
问题 I tried to forward-declare a constexpr variable template like this: template<typename> constexpr std::size_t iterator_category_value; The goal was to document that every specialization should be constexpr but I have to admit that I never checked whether it was legal or not and g++ was happy with it. However, when I tried to compile this spinnet with clang++ instead, I got the following error: error: default initialization of an object of const type 'const std::size_t' (aka 'const unsigned

Constexpr is not allowed in declaration of friend template specialization?

拈花ヽ惹草 提交于 2019-12-23 06:44:07
问题 I'm porting a C++14- constexpr codebase from Clang to the latest g++-5.1. Consider the following reduced code snippet of a home-grown bitset class that has been compiling correctly since the halcyon days of Clang 3.3 (almost 2 years now!) #include <cstddef> template<std::size_t> class bitset; template<std::size_t N> constexpr bool operator==(const bitset<N>& lhs, const bitset<N>& rhs) noexcept; template<std::size_t N> class bitset { friend constexpr bool operator== <>(const bitset<N>&, const

Truncate a String at Compile-Time

你离开我真会死。 提交于 2019-12-23 03:39:27
问题 I have a string literal with a value that is out of my control (for example a #define in a config.h file) and I want to initialize a global fixed-size character array with it. If the string is too long, I want it to be truncated. Basically, what I want to achieve is the effect of #define SOMETEXT "lorem ipsum" #define LIMIT 8 char text[LIMIT + 1]; std::strncpy(text, SOMETEXT, LIMIT); text[LIMIT] = '\0'; except that I cannot use this code because I want text to be a statically initialized

constexpr does not work/apply inside function call

早过忘川 提交于 2019-12-23 03:25:16
问题 I have implemented a constexpr compile-time hash-function, which works fine (i.e. is evaluated at compile-time) if called as constexpr auto hash = CompileTimeHash( "aha" ); but I need to use it in actual code as an argument to a function as in foo( CompileTimeHash( "aha" ) ); // foo is NOT constexpr For a specific reason, I cannot use the long version constexpr auto hash = CompileTimeHash( "aha" ); foo( hash ); The compiler (VC++) will not compile-time hash in the short (first) case. Is there