Scope resolution in needed to call a functor maker

H

Howard Gardner

/*
I don't have to provide scope resolution in sit 0 below: adl
finds the function that I mean to use.

I do have to provide scope resolution in sits 1 and 2.

Can anyone teach me a trick to get around the need for scope resolution
in sit 1 or sit 2?

sit 1 is my motivating case, but if there's a trick for sit 2 I'd like
to know it too.
*/

#include <iostream>
using namespace std;

// minimalist functor
template < typename xCallable, typename xResult >
struct ftor
{
typedef xCallable tCallable;
typedef xResult tResult;
ftor(const ftor & fThat):cCallable(fThat.cCallable){}
explicit ftor(tCallable fCallable):cCallable(fCallable){}
template < typename xArg > tResult operator()(xArg fArg) const
{return cCallable(fArg);}
tCallable cCallable;
};

// functor maker
template < typename xResult, typename xArg >
ftor< xResult (*)(xArg), xResult >
make_ftor(xResult (*fPtrFunc)(xArg))
{return ftor< xResult (*)(xArg), xResult >(fPtrFunc);}

// class and function that uses it
namespace A
{
class a{};
a func(a fA){cout << "::A::func" << endl; return fA;}
}

int main()
{
typedef A::a tA;
typedef tA (*tPtrFunc)(tA);

tA fA;

// sit 0 - Finds through adl
func(fA);

// sit 1 - Insists on scope resolution
make_ftor(A::func)(fA);

// sit 2 - Insists on scope resolution
tPtrFunc fPtrFunc = A::func;
fPtrFunc(fA);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top