static-assert

decltype( constexpr variable)

女生的网名这么多〃 提交于 2021-02-16 16:13:27
问题 Why decltype of constexpr variable is failed ? #include <cstdint> #include <type_traits> constexpr uint16_t foo(){ return 0;} constexpr auto cv = foo(); auto v = foo(); static_assert( std::is_same< uint16_t, decltype(cv)>::value, "!"); // failed static_assert( std::is_same< uint16_t, decltype(v) >::value, "!"); // success 回答1: decltype(entity) specifies the declared type of the entity specified by this expression. Due to the constexpr, ( A constexpr specifier used in an object declaration

decltype( constexpr variable)

依然范特西╮ 提交于 2021-02-16 16:13:00
问题 Why decltype of constexpr variable is failed ? #include <cstdint> #include <type_traits> constexpr uint16_t foo(){ return 0;} constexpr auto cv = foo(); auto v = foo(); static_assert( std::is_same< uint16_t, decltype(cv)>::value, "!"); // failed static_assert( std::is_same< uint16_t, decltype(v) >::value, "!"); // success 回答1: decltype(entity) specifies the declared type of the entity specified by this expression. Due to the constexpr, ( A constexpr specifier used in an object declaration

static_assert for unique_ptr of any type

烂漫一生 提交于 2021-02-07 13:46:12
问题 How can I statically assert that an expression is a std::unique_ptr i.e. std::unique_ptr<T> for any T . static_assert (std::is_pointer<decltype(exp)>()), "not a smart pointer") Above does not work. If nothing straight forward, I am only interested if bool() operator is defined for the type. 回答1: Create your own trait, with the appropriate partial specialisation: template <class T> struct is_unique_ptr : std::false_type {}; template <class T, class D> struct is_unique_ptr<std::unique_ptr<T, D>

static_assert for unique_ptr of any type

断了今生、忘了曾经 提交于 2021-02-07 13:45:39
问题 How can I statically assert that an expression is a std::unique_ptr i.e. std::unique_ptr<T> for any T . static_assert (std::is_pointer<decltype(exp)>()), "not a smart pointer") Above does not work. If nothing straight forward, I am only interested if bool() operator is defined for the type. 回答1: Create your own trait, with the appropriate partial specialisation: template <class T> struct is_unique_ptr : std::false_type {}; template <class T, class D> struct is_unique_ptr<std::unique_ptr<T, D>

static_assert if expressions is constexpr

不问归期 提交于 2021-02-06 09:44:29
问题 I want to create a class template template <class T> class X { // here I'll use T::value (among other things) }; T::value will often be a constexpr static variable, but not always. T::value has to be positive value, so I want to let people know it during compilation, when possible. If T::value was always constexpr, I'd add static_assert like static_assert(T::value > 0, "need positive number"); Is it possible to add this static_assert only for cases when T::value is constexpr? 回答1: We can

C++ static assert of IEEE754

烂漫一生 提交于 2021-01-28 06:37:40
问题 ) How to make a static assert of IEEE754 norm (floating point representation)? My idea was something like that: static unsigned char c[8] = { 0, 0, 0, 0, 0, 0xd0, 0x84, 0x40 }; static double d= *reinterpret_cast<double *>(c); BOOST_STATIC_ASSERT(d==666.); But it doesnt't work :( I should point out that my compiler is not C++11 (I use visual studio 2008) and I don't have regular static asserts. 回答1: First, note that due to compiler idiosyncrasies you can't reliably assert that floating point

std::cout equivalent at compile time, or static_assert stringification of compile-time constant values in c++11

烂漫一生 提交于 2020-06-24 07:14:56
问题 Is there a way to print the value of a constexpr or #define d value at compile time? I want the equivalent of std::cout << , or some way to do something like constexpr int PI_INT = 4; static_assert(PI_INT == 3, const_str_join("PI_INT must be 3, not ", const_int_to_str(PI_INT))); Edit: I can do some basic compile-time printing with constexpr s, at least on gcc by doing something like template <int v> struct display_non_zero_int_value; template <> struct display_non_zero_int_value<0> { static

static_assert inside/outside class definition

时光总嘲笑我的痴心妄想 提交于 2020-06-22 11:10:52
问题 Why does static_assert need to be out side of the class definition? Failing code #include <type_traits> class A { public: A(A&&) noexcept {} static_assert(std::is_nothrow_move_constructible<A>::value, "ERROR"); }; int main() { } Working code #include <type_traits> class A { public: A(A&&) noexcept {} }; static_assert(std::is_nothrow_move_constructible<A>::value, "ERROR"); int main() { } And when is it appropriate to use static_asserts in the definition of a class or struct? 回答1: As far as the

Use static_assert to check types passed to macro

拜拜、爱过 提交于 2020-04-05 11:50:26
问题 I unfortunately have several macros left over from the original version of my library that employed some pretty crazy C. In particular, I have a series of macros that expect certain types to be passed to them. Is it possible to do something along the lines of: static_assert(decltype(retval) == bool); And how? Are there any clever alternatives? Yes I'm aware macros are bad. I'm aware C++ is not C, etc. Update0 Here is some related code, and the source file. Suggestions are welcome. The

static_assert in function template with non-type template parameter

让人想犯罪 __ 提交于 2020-02-07 05:14:06
问题 I have a function template with integer template parameter. I would like to provide an implementation for particular integers only. An attempt to use the function template with another argument should cause a compilation error. I used static_assert in a way presented below. #include <type_traits> #include <iostream> template <typename T> struct false_type : public std::false_type {}; template <int T> void function() { static_assert(false_type<decltype(T)>::value, "Error"); }; template <> void