Which is preferred? Static assertions with type traits or concept_check?
Hi all, I'm writing a template class and I have several requirements on the type parameters. A lot of it has to do with the inter-convertibility of the types. I have 2 options to choose from as to how I can check for these convertibility requirements: static assertions with the type traits selector is_convertible, or using concept_check to check if the assignment operation is defined for the types. Even though the former leads to shorter code, the latter works just as well as I already have concepts modeled for these types, so I could just dump the inter-convertibility checks inside as well without much hassle. In summary, I either choose this: BOOST_STATIC_ASSERT((boost::is_convertible<T1, T2>::value)); or this: template<class T1, class T2> class Concept { public: void constraints() { // insert other constraints a = b; b = a; } T1 a; T2 b; }; Thanks a lot, and have a good day Benjamin Lau
On Apr 2, 2007, at 7:20 AM, Benjamin Lau wrote:
I'm writing a template class and I have several requirements on the type parameters. A lot of it has to do with the inter-convertibility of the types. I have 2 options to choose from as to how I can check for these convertibility requirements: static assertions with the type traits selector is_convertible, or using concept_check to check if the assignment operation is defined for the types. Even though the former leads to shorter code, the latter works just as well as I already have concepts modeled for these types, so I could just dump the inter-convertibility checks inside as well without much hassle.
If you already have the concepts for these types, then I would suggest sticking with just the concept check library. BOOST_STATIC_ASSERT is good for simple checks, but its use doesn't really scale to non-trivial concepts. Cheers, Doug
participants (2)
-
Benjamin Lau
-
Doug Gregor