views:

72

answers:

3

Hi,

Suppose you have a certain template that takes a parameter class

template <typename ConnectorClass>
struct myClass {

}

I want to add a BOOST_ASSERT_MSG to validate that ConnectorClass implements a certain method of signature

returnType MethodName(param1, param2)

How should i write the assert condition in this case?

EDIT: since the question does not seem to have a clear solution, i'm posting a sub-question with intermediate results based on some references on the answers please follow here

A: 

If you are on Windows, and not too picky, you can do this via __if_exists, a hack extension to Visual C++

Steve Townsend
lurscher
@lurscher - better to do it more cleanly anyway, if possible. There's another discussion here that might be useful: http://programming.itags.org/c-c++/72176/
Steve Townsend
+2  A: 

You can't. BOOST_ASSERT_MSG is evaluated during runtime. If the class doesn't have that member, you'll get a compilation error wherever your template expects it.

If your goal is better error messages, look into Boost's Concept Check library. If your goal is to have your template do something different depending on what members are available, look into Boost's enable_if.

Here's an answer to a similar question.

Steve M
hmm, i'm pretty sure that BOOST_ASSERT_MSG is evaluated at compile-time, however i didn't knew about the Concept Check library, worth a check, thanks!
lurscher
You may be thinking of BOOST_STATIC_ASSERT.
Steve M
A: 

Look into the various static assertion utilities in Boost. In particular:

  • BOOST_STATIC_ASSERT (see docs here)
  • BOOST_MPL_ASSERT (see docs here)
GrafikRobot