static-assert

static_assert fails compilation even though template function is called nowhere

人走茶凉 提交于 2019-11-26 17:35:53
I use g++ 4.6.3, (currently default package for ubuntu 12.04) with the flag c++0x, and I stumble across this: template <typename T> inline T getValue(AnObject&) { static_assert(false , "this function has to be implemented for desired type"); } with the compilation error: static_assertion failed "this function has to be implemented for the desired type" even though I don't call this function anywhere yet . Is it a g++ bug ? Shouldn't this function be instanciated only if it is called somewhere in the code. That's because the condition does not depend in any way on the template parameters.

Ways to ASSERT expressions at build time in C

守給你的承諾、 提交于 2019-11-26 15:09:49
I'm tidying up some older code that uses 'magic numbers' all over the place to set hardware registers, and I would like to use constants instead of these numbers to make the code somewhat more expressive (in fact they will map to the names/values used to document the registers). However, I'm concerned that with the volume of changes I might break the magic numbers. Here is a simplified example (the register set is more complex): const short mode0 = 0; const short mode1 = 1; const short mode2 = 2; const short state0 = 0; const short state1 = 4; const short state2 = 8; so instead of : set

How to do static_assert with macros?

こ雲淡風輕ζ 提交于 2019-11-26 14:50:14
问题 I have tried to use this suggestion to do a static assert, but I do not get a compilation error if I use it within a method of a template. The example follows : #include <iostream> #define STATIC_ASSERT(expr, msg) \ { \ char STATIC_ASSERTION__##msg[(expr)?1:-1]; \ (void)STATIC_ASSERTION__##msg[0]; \ } template <typename T > class A { public: int foo(const int k ) { // does not work STATIC_ASSERT( k > 9, error_msg ); return k+5; } }; int bar(const int k ) { // works fine //STATIC_ASSERT( k > 9

constexpr if and static_assert

不问归期 提交于 2019-11-26 11:29:00
问题 P0292R1 constexpr if has been included, on track for C++17. It seems useful (and can replace use of SFINAE), but a comment regarding static_assert being ill-formed, no diagnostic required in the false branch scares me: Disarming static_assert declarations in the non-taken branch of a constexpr if is not proposed. void f() { if constexpr (false) static_assert(false); // ill-formed } template<class T> void g() { if constexpr (false) static_assert(false); // ill-formed; no // diagnostic required

static_assert fails compilation even though template function is called nowhere

醉酒当歌 提交于 2019-11-26 05:29:15
问题 I use g++ 4.6.3, (currently default package for ubuntu 12.04) with the flag c++0x, and I stumble across this: template <typename T> inline T getValue(AnObject&) { static_assert(false , \"this function has to be implemented for desired type\"); } with the compilation error: static_assertion failed \"this function has to be implemented for the desired type\" even though I don\'t call this function anywhere yet . Is it a g++ bug ? Shouldn\'t this function be instanciated only if it is called

C++11 - static_assert within constexpr function?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 04:46:42
问题 How would one properly do a static_assert within a constexpr function? For example: constexpr int do_something(int x) { static_assert(x > 0, \"x must be > 0\"); return x + 5; } This is not valid C++11 code, because a constexpr function must only contain a return statement. I don\'t think that the standard has an exception to this, although, the GCC 4.7 does not let me compile this code. 回答1: This is not valid C++11 code, because a constexpr function must only contain a return statement. This

Static assert in C

天大地大妈咪最大 提交于 2019-11-26 00:28:19
问题 What\'s the best way to achieve compile time static asserts in C (not C++), with particular emphasis on GCC? 回答1: 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 also implemented