boost_assert that a parameter class implements a certain method
Sup开发者_开发知识库pose 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
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.
If you are on Windows, and not too picky, you can do this via __if_exists, a hack extension to Visual C++
Look into the various static assertion utilities in Boost. In particular:
- BOOST_STATIC_ASSERT (see docs here)
- BOOST_MPL_ASSERT (see docs here)
精彩评论