[mpl] how to find out whether a function is implemented
I hope the MPL label is right. At least the question has *some* relevance to metaprogramming. Basically, I need to find out whether a certain function F over a certain type is defined, and if yes, take its address. Input: the type T, and function NAME F. Output: pointer to F if Ret F(T, T& or T const&; or two arguments) is defined, 0 otherwise. The function can return anything, including void. Variations in input: - the function may take T, T&, or T const& - if possible, the method should work for user-defined operators (my C++ is a bit rusty in this area - is it at all possible to take an address of an overloaded operator?) I guess the F would then be named, e.g. 'operator<' ? If the general case is hard to work out, it's OK to have the solution just for Ret F(T const&) and Ret F(T const&, T const&). I hope the question is clear enough :) Thanks for your time and answers.
Zeljko Vrba
I hope the MPL label is right. At least the question has *some* relevance to metaprogramming.
Basically, I need to find out whether a certain function F over a certain type is defined, and if yes, take its address.
I can help you with the first part: http://www.boost-consulting.com/boost/boost/detail/is_incrementable.hpp shows an example determining if ++x is defined. However, reliably taking the address of an overloaded function involves knowing the exact types in its signature. I'm not sure how you can do that. -- Dave Abrahams Boost Consulting www.boost-consulting.com
David Abrahams wrote:
However, reliably taking the address of an overloaded function involves knowing the exact types in its signature. I'm not sure how you can do that.
I have googled a bit and found the following example in the Microsoft C++ reference: http://tinyurl.com/cljnn int Func( int i, int j ); int Func( long l ); ... int (*pFunc) ( int, int ) = Func; Do you see any problems with this? I guess it should be possible to take an address of an operator by replacing 'Func' with e.g. 'operator<'. I'll try it and report the result.
Zeljko Vrba
David Abrahams wrote:
However, reliably taking the address of an overloaded function involves knowing the exact types in its signature. I'm not sure how you can do that.
I have googled a bit and found the following example in the Microsoft C++ reference: http://tinyurl.com/cljnn
int Func( int i, int j ); int Func( long l );
...
int (*pFunc) ( int, int ) = Func;
Do you see any problems with this?
Not at all. As I said, you need to know the exact signature. The
problem is detecting that the function (or operator<) whose existence
you detect has the exact signature you care about.
Maybe it could be tested by passing a convertible-to-T POD struct
through the function:
// UNTESTED
template <class T>
struct convertible_to { operator T() const; };
namespace tester
{
typedef char (&yes) [1];
typedef char (&no) [2];
struct not_found {};
no is_found(not_found);
yes i_found(...);
not_found operator<(...);
template
I guess it should be possible to take an address of an operator by replacing 'Func' with e.g. 'operator<'. I'll try it and report the result.
I don't think this problem can be solved. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams
-
Zeljko Vrba