Use static_assert to check types passed to macro
I found this to be the cleanest, using @UncleBens suggestion: #include <type_traits> static_assert(std::is_same<decltype(retval), bool>::value, “retval must be bool”);
I found this to be the cleanest, using @UncleBens suggestion: #include <type_traits> static_assert(std::is_same<decltype(retval), bool>::value, “retval must be bool”);
In your first example, static_assert should take a second parameter which would be a string literal, otherwise it’s deemed to fail (edit: dropping the the second parameter is legal since C++17). And this second argument cannot be defaulted. Your second example is incorrect for several reasons: decltype is meant to be used on an expression, … Read more
Since C++11, you can check the size during compilation: static_assert (sizeof(mystruct) == 1024, “Size is not correct”); Pre-c++11 compilers, Boost has a workaround: BOOST_STATIC_ASSERT_MSG(sizeof(mystruct) == 1024, “Size is not correct”); See the documentation.
The standard says in [temp.res]/8 No diagnostic shall be issued for a template definition for which a valid specialization can be generated. If no valid specialization can be generated for a template definition, and that template is not instantiated, the template definition is ill-formed, no diagnostic required. … [ Note: If a template is instantiated, … Read more
This is not valid C++11 code, because a constexpr function must only contain a return statement. This is incorrect. static_assert in a constexpr function are fine. What is not fine is using function parameters in constant expressions, like you do it. You could throw if x <= 0. Calling the function in a context that … Read more
This is talking about a well-established rule for templates – the same rule that allows compilers to diagnose template<class> void f() { return 1; }. [temp.res]/8 with the new change bolded: The program is ill-formed, no diagnostic required, if: no valid specialization can be generated for a template or a substatement of a constexpr if … Read more
My Hack Code: template <typename Assertion> struct AssertValue : AssertionChecker<Assertion::value, Assertion> { static_assert(AssertionValue, “Assertion failed <see below for more information>”); static bool const value = Assertion::value; }; It allows for you to check any ::value assertion and dump the types if it failed. Usage: // Bad indentation used to show parts static_assert( AssertValue< std::my_check< T0, … Read more
C11 standard adds the _Static_assert keyword. This is implemented since gcc-4.6: _Static_assert (0, “assert1”); /* { dg-error “static assertion failed: \”assert1\”” } */ The first slot needs to be an integral constant expression. The second slot is a constant string literal which can be long (_Static_assert(0, L”assertion of doom!”)). I should note that this is … Read more
Static assert is used to make assertions at compile time. When the static assertion fails, the program simply doesn’t compile. This is useful in different situations, like, for example, if you implement some functionality by code that critically depends on unsigned int object having exactly 32 bits. You can put a static assert like this … Read more