Re: Sun CC compiler: function overloads



Thomas Maier-Komor wrote:
Rather not. Some kind of cast should resolve the overloaded
function. Cf. ISO/IEC 14882:1998(E), 13.4 "Address of overloaded
function":

A use of an overloaded function name without arguments is resolved
in certain contexts to a function, a pointer to function or a
pointer to member function for a specific function from the overload
set. A function template name is considered to name a set of
overloaded functions in such contexts. The function selected is the
one whose type matches the target type required in the context. The
target can be
[...]
-- an explicit type conversion (5.2.3, 5.2.9, 5.4).

So basically, at least some kind of cast is supposed work. Explicit
type cast does, and I think reinterprete_cast does either. Static
cast apparently doesn't with Sun CC, so the question is whether Sun
engineers had a specific reason not to implement it.

Considering the quoted section, and especially the reference section
5.2.9 (static cast), rejecting the code is a bug. So someone should open
up a case so that this issue could be fixed in the future. In my
experience they are always happy getting bugreports, especially if it
concerns inconsistency between CC and g++.

Thank you all for these helpful answers. I tried the old c-style-cast
and it works fine.

As far as the design is concerned, I use this construct for a visitor
pattern implementation. I print a rather simplified version below, with
many details like ctors, dtors, etc missing. In case you have a
suggestion, it is of course welcome.

void add (int, int, bool);
void add (int, int);

void sub (int, int);

class term;

struct visitor
{
virtual void constant (int val) {}
virtual void variable (std::string var) {}
virtual void binary_op (void (*func) (int, int), term *a, term *b) {}
};

struct term
{
virtual void visit (visitor &v) = 0;
};

struct constant : public term
{
int val;
void visit (visitor &v)
{
v.constant (val);
}
};

struct variable : public term
{
std::string var;
void visit (visitor &v)
{
v.variable (var);
}
};

struct binary_op : public term
{
void (*func) (int, int);
term *a;
term *b;
void visit (visitor &v)
{
v.binary_op (func, a, b);
}
};

struct printer : public visitor
{
std::ostream &os;
virtual void constant (int val)
{
os << val;
}
virtual void variable (std::string var)
{
os << var;
}
virtual void binary_op (void (*func) (int, int), term *a, term *b)
{
a->visit (*this);
if (static_cast <void (*) (int, int)> (add) == func)
os << "+";
else if (static_cast <void (*) (int, int)> (sub) == func)
os << "-";
else
os << "<unknown>";
b->visit (*this);
}
};
.



Relevant Pages