visitor woe

I

Imre Palik

Hi,

I am trying to create a framework, that automatically generates a base
class for the visitor pattern:

template <typename param, typename ret = void>
struct visitor
{
typedef ret return_type;
virtual return_type visit(param *) = 0;
};

template <class head, class tail, typename ret>
struct visitor<type_cons<head, tail>, ret>
: public visitor<head, ret>, public visitor<tail, ret>
{ typedef ret return_type; };

template <class head, typename ret>
struct visitor<type_cons<head, null_type>, ret>
: public visitor<head, ret>
{ typedef ret return_type; };

But if I instantiate this type with

typedef make_typelist<unsigned, long double, string>::result tlist;
typedef visitor<tlist, void> visit;

and use it via

void accept (visit &vis)
{
unsigned u = 1;
vis.visit(&u);
}

gcc says:

In function `void accept(visit&)':
error: request for member `visit' is ambiguous
error: candidates are: ret visitor<param, ret>::visit(param*) [with param = std::string, ret = void]
error: ret visitor<param, ret>::visit(param*) [with param = long double, ret = void]
error: ret visitor<param, ret>::visit(param*) [with param = unsigned int, ret = void]

I am totaly out of ideas how to persuade gcc to choose the one that
expects an unsigned * for parameter. Any ideas?

Thx

ImRe

P.S.: gcc -dumpversion 3.4.0
 
M

Maxim Yegorushkin

Imre Palik wrote:

[]

The problem you are experiencing is caused by name hiding, you might
like to google for it.

The fix is easy:
template <class head, class tail, typename ret>
struct visitor<type_cons<head, tail>, ret>
: public visitor<head, ret>, public visitor<tail, ret>
{
using visitor<head, ret>::visit;
using visitor<tail, ret>::visit;
typedef ret return_type;
};
template <class head, typename ret>
struct visitor<type_cons<head, null_type>, ret>
: public visitor<head, ret>
{
using visitor<head, ret>::visit;
typedef ret return_type;
};
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top