
David Abrahams wrote:
// Only the specializations (below) of this template will compile. template <int N> static foo set() { return invalid_value; }
Without a definition for invalid_value, this is invalid code, and on a conforming compiler, compilation fails at the point it is parsed.
Thanks again. g++ 3.3 appears to happy differentiate between "valid" and "invalid" enum values with the code below, so allow me to rephrase my earlier question: which of the two techniques we've discussed would you use? I assume that one has technical merits over the other? Regards, Angus #include <boost/static_assert.hpp> #include <iostream> class foo { public: enum state { state1, state2, state3 }; // Only the specializations (below) of this template will compile. template <int N> static foo set() { BOOST_STATIC_ASSERT( N == state2 ); } private: foo(state) { std::cout << "foo" << std::endl; } }; template <> foo foo::set<foo::state2>() { return foo::state2; } template <> foo foo::set<foo::state3>() { return foo::state3; } int main() { // Compiles, as expected. foo f1 = foo::set<foo::state2>(); foo f2(foo::set<foo::state2>()); // Fail to compile, as expected. foo f3 = foo::set<foo::state1>(); // foo f4(foo::set<foo::state1>()); return 0; }