I want to define a class whose instances can be built, implicitly built, or assigned from an integer constant zero, but not from any other numeric constant, and not from a variable with an integer type (even if its value is zero at run time). It should also be available for copying from other instances of the same class. Using the features of C ++ 11 is in order if they are supported (in the appropriate mode) by both g ++ 4.6 and MSVC 2010.
Specifically, this
class X { }; void fn(X);
they should all compile:
X a(0); X b = 0; X c; c = 0; X d = a; X e; e = a; fn(0);
but this should not:
X f(1); X g = 1; X h; h = 1; fn(1); int ii = 23; X a(ii); X j = ii; X k; k = ii; fn(ii);
I tried this, but this did not work:
class X { public: X() {} constexpr X(int v) { static_assert(v == 0, "must be initialized from zero"); } };
& Longrightarrow;
test.cc: In constructor 'constexpr X::X(int)': test.cc:3:29: error: non-constant condition for static assertion test.cc:3:29: error: 'v' is not a constant expression
source share