开发者

C++ type traits to check if class has operator/member [duplicate]

This question already has answers here: 开发者_如何转开发 Closed 10 years ago.

Possible Duplicate:

Is it possible to write a C++ template to check for a function's existence?

Is it possible to use boost type traits or some other mechanism to check if a particular template parameter has an operator/function, e.g. std::vector as a template parameter has operator[], while std::pair does not.


You can't solve this via type traits because you'd have to define if for every possible name.

Here are the common solutions listed, which have one problem though: many STL implementations put common code in base classes and this method doesn't check for inherited names.

If you need to check for inherited members too, see here. The answer provides a solution that checks whether the class in question has a member of that name and can also check for const-ness and argument count. It fails however to check for the full signature including argument and return types and member visibility doesn't make a difference. You should be able to solve that partially by using the linked is_call_possible<> (haven't had time yet too look at it).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜