Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-08-05 13:05:50


Zeljko Vrba <zvrba_at_[hidden]> writes:

> 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 <class A1, class A2>
     struct has_less1
     {
         static bool const value
           = sizeof(
               is_found(
                  convertible_to<A1>() < convertible_to<A2>()
               )
             ) == yes;
     };

     template <class T> T make();

     template <class T, class U>
     no is(T&,U&);

     template <class T>
     yes is(T&,T&);

     template <class R, class A1, class A2, bool = has_less1<A1,A2>::value>
     struct has_less
     {
         static bool const value
           = sizeof( is(make<R>(), make<A1>() < make<A2>()) ) == yes;
     };

     template <class R, class A1, class A2>
     struct has_less<R,A1,A2,false>
     {
         static bool const value = false;
     };
   }

The problem with this is that you still can't tell whether the
operator< you found takes its arguments by reference or by value, and
without that information, you can't do the cast you want reliably.

> 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

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net