In the following, GCC confuses template struct name
with template member function name
of class A
, while Clang compiles fine (live example):
template<typename T>
struct name {};
struct A
{
template<bool B>
void name() { }
};
template<bool B, typename T>
void f(T& x) { x.template name<B>(); }
Function f
is apparently meant to be called with an argument of type A
in this example, but it could be anything else, so f
needs to remain a template function.
I don't care much which compiler is correct, I only need a work-around because I really don't know any syntax other than
x.template name<B>();
to call the member function, and I cannot see how a using
declaration or any other way of disambiguation could apply.
EDIT Yes, I now tried the more explicit syntax
x.T::template name<B>();
which works, but is really ugly. Any way to make the brief syntax work? Otherwise, it might be preferable to change one of the two names to begin with...
EDIT2 My original version of f
works on a universal reference T&&
, which needs the ugliest
using X = typename std::remove_reference<T>::type;
x.X::template name<B>();
in case T
is a reference... And all this for a simple function call.
A
and invokesa.name<B>
? Override for&
,const&
, and&&
if needed. – Highspeedf
exactly this function? Oh, you mean take anA
and not a template argument? This is nearly impossible, it could be called with anything...A
was just an example. – Skywayvoid f(T& x) { x.name<B>(); }
? – Edvax
's type,T
, is unknown. Whenf
is first parsed, how would the compiler know thatname
isn't a plain data member ofx
? This would give expressionx.name
followed by operator<
followed byB
( just abool
), followed by operator>
, followed by empty pararetheses, at which point you getexpected primary-expression
. Besides, in this example,::name
gets in the way before all of this happens. – Skywayname
isn't a plain data member of x: It knows this, because at the point where f is instantiated, it has to know the definition of T anyway and thus knows thatname
is a templated member function. – Edva