I am trying to use a pre-C++11 static assert. I found this and this question, but somehow I cant get it running:
#define STATIC_ASSERT(x) \ do { \ const static char dummy[(x)?1:-1] = {0};\ } while(0) struct bar { int value; template<typename T> void setValue(T x); }; template<typename T> void bar::setValue(T x) { STATIC_ASSERT(1==0); } template<> void bar::setValue(int x) { value = x;} int main(){ bar b; int c = 1; b.setValue(c); } Compiling this (gcc) results in
error: size of array 'dummy' is negative
I would expect this error to apprear only if I call setValue with anything other than int. I also tried other proposed solutions, but with more or less the same result: The error is there even if I dont instantiate the template with anything other than int. What am I doing wrong?
inttype? (It's still interesting question and answer for educational reasons, but I hope your real usage is not as trivial :) ).explicitit allows me to avoid conversions of one particular type (whose code I wrote) but I dont see how this would help to avoid conversions taking place for the parameter of one particular function (e.g.doubletoint). Moreoverexplicitis C++11 but I am looking for a C++03 solution