Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 822
Anycorn Avatar asked Jan 23 '10 06:01

Anycorn


1 Answers

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).

like image 137
Georg Fritzsche Avatar answered Nov 07 '22 08:11

Georg Fritzsche