
hi, I would like to avoid template instantiations with ContractType whose static functions st1 and st2 return an enum... template <typename ContractType> class C { BOOST_STATIC_ASSERT( !(ContractType::st1()==A && ContractType::st2()==P) ); ... }; Is there a way to make this work?

AMDG Hicham Mouline wrote:
hi,
I would like to avoid template instantiations with ContractType whose static functions st1 and st2 return an enum...
template <typename ContractType> class C { BOOST_STATIC_ASSERT( !(ContractType::st1()==A && ContractType::st2()==P) ); ... };
Is there a way to make this work?
You can't use static functions. st1 and st2 have to be either enumerators or static const /integral types/ template <typename ContractType> class C { BOOST_STATIC_ASSERT( !(ContractType::st1==A && ContractType::st2==P) ); ... }; In Christ, Steven Watanabe

I am not sure if it is possible to get the return type of a function so as to use it as a template type parameter or atleast I don't know how to do that. So, I have a work-around wherein the ContractType's define typedef's for the return types of the two functions st1() and st2(). What I have is something like this: struct Contract1{ enum myenum {myenum_1, myenum_2}; typedef myenum st1_return_type; typedef myenum st2_return_type; static myenum st1(){return myenum_1;}; static myenum st2(){return myenum_2;}; }; struct Contract2{ typedef int st1_return_type; typedef int st2_return_type; enum myenum {myenum_1, myenum_2}; static int st1(){return myenum_1;}; static int st2(){return myenum_2;}; }; template <typename ContractType> class Test { BOOST_STATIC_ASSERT(boost::is_enum<typename ContractType::st1_return_type>::value && boost::is_enum<typename ContractType::st2_return_type>::value); void otherMembers(){} }; int main(){ Test<Contract1> obj1; //uncomment to get the assert //Test<Contract2> obj2; } You should get the error even if some ContractType doesn't defined those typedefs. But the valid classes (for which st1() and st2() don't return enums) should strictly define them. The above works for me with VC++ 2005. (include boost/type_traits/is_enum.hpp) Thanks and regards, Abhishek Padmanabh -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Hicham Mouline Sent: 29 February 2008 18:54 To: boost-users@lists.boost.org Subject: [Boost-users] static assert hi, I would like to avoid template instantiations with ContractType whose static functions st1 and st2 return an enum... template <typename ContractType> class C { BOOST_STATIC_ASSERT( !(ContractType::st1()==A && ContractType::st2()==P) ); .... }; Is there a way to make this work? _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hi,
I suggest the following solution:
============================================================
template<class F>
void testEnum(const F & f)
{
BOOST_STATIC_ASSERT(!boost::is_enum ::result_type >::value);
} template <typename ContractType>
class C {
private:
static void verify()
{
testEnum(&ContractType::st1);
testEnum(&ContractType::st2);
}
public:
~C() { verify(); }
};
============================================================
The problem is "verify" function should be generated during compilation.
If class "C" contains only static functions then you will have to invoke
"verify" from all of them, to guarantie that verify will be generated.
In other case it is enough to invoke "verify" from destructor.
Best Regards,
Sergei

I've improved my solution by:
==============================================================
template<class F>
void testEnum(const F & f)
{
BOOST_STATIC_ASSERT(!boost::is_enum ::result_type >::value);
} template
participants (4)
-
Abhishek Padmanabh
-
Hicham Mouline
-
Sergei Politov
-
Steven Watanabe