This code fails to compile on g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3, with this error
test.cpp: In function ‘T mul(V&, V&)’:
test.cpp:38:27: error: expected primary-expression before ‘>’ token
test.cpp:38:29: error: expected primary-expression before ‘)’ token
test.cpp:38:53: error: expected primary-expression before ‘>’ token
test.cpp:38:55: error: expected primary-expression before ‘)’ token
but it compiles and executes correctly on Microsoft C/C++ Optimizing Compiler Version 15.00.21022.08 for x64
#include <iostream>
#include <complex>
template <class T>
class SM
{
public:
T value;
};
template <class T>
class SC : public SM<T>
{
};
class PSSM {
public:
template <class T>
T & getSC() { return sc; }
private:
SC<double> sc;
};
class USSM {
public:
template <class T>
T & getSC() { return sc; }
private:
SC<std::complex<double> > sc;
};
template <class T, class V>
T mul( V & G, V & S) {
return (G.getSC<SC<T> >().value * S.getSC<SC<T> >().value); // error is here
}
int main() {
PSSM p;
PSSM q;
p.getSC<SC<double> >().value = 5;
q.getSC<SC<double> >().value = 3;
std::cout << mul<double>(p,q);
}
I don't understand where the problem is. Can anyone understand how to work around it, or explain the nature of the problem in g++?
The problem is syntactic. You should use the template disambiguator in this case, so that your invocation of a member function template will be correctly parsed:
return (G.template getSC<SC<T> >().value * S.template getSC<SC<T> >().value);
// ^^^^^^^^^ ^^^^^^^^^
This disambiguator helps the compiler recognizing that what follows G. is a member template specialization and not, for instance, a data member called getSC followed by a < (less than).
The Standard reference for the template disambiguator is Paragraph 14.2/4 of the C++11 Standard:
When the name of a member template specialization appears after . or -> in a postfix-expression or after a nested-name-specifier in a qualified-id, and the object expression of the postfix-expression is type-dependent or the nested-name-specifier in the qualified-id refers to a dependent type, but the name is not a member of the current instantiation (14.6.2.1), the member template name must be prefixed by the keyword template. Otherwise the name is assumed to name a non-template.[ Example:
struct X {
template<std::size_t> X* alloc();
template<std::size_t> static X* adjust();
};
template<class T> void f(T* p) {
T* p1 = p->alloc<200>(); // ill-formed: < means less than
T* p2 = p->template alloc<200>(); // OK: < starts template argument list
T::adjust<100>(); // ill-formed: < means less than
T::template adjust<100>(); // OK: < starts template argument list
}
—end example ]
Related
This doesn't compile:
template<class X> struct A {
template<int I> void f() {}
};
template<class T> void g()
{
A<T> a;
a.f<3>(); // Compilation fails here (Line 18)
}
int main(int argc, char *argv[])
{
g<int>(); // Line 23
}
The compiler (gcc) says:
hhh.cpp: In function 'void g()':
hhh.cpp:18: error: expected primary-expression before ')' token
hhh.cpp: In function 'void g() [with T = int]':
hhh.cpp:23: instantiated from here
hhh.cpp:18: error: invalid use of member (did you forget the '&' ?)
Can anyone explain why this is? Is there a way to get it to work?
Try the following code:
template<class T> void g()
{
A<T> a;
a.template f<3>(); // add `template` keyword here
}
According to C++'03 Standard 14.2/4:
When the name of a member template specialization appears after . or -> in a postfix-expression, or after nested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on a template-parameter (14.6.2), the member template name must be prefixed by the keyword template. Otherwise the name is assumed to name a non-template.
Future C++ Standard seems to be still require this keyword according to draft n2857 14.3/4. Some compilers has special mode that allows to compile original code without errors (Comeau compiles it in so called relaxed mode).
I try to compile the following C++ code:
struct A {
template< bool x >
bool fun() {
return x;
}
};
template< typename T >
struct B {
struct A2 {
template< bool x >
bool fun() {
return x;
}
};
void test() {
A a;
A2 a2;
a.fun< true >();
a2.fun< true >();
}
};
The compiler complains that:
source_file.cpp: In member function ‘void B<T>::test()’:
source_file.cpp:22:24: error: expected primary-expression before ‘)’ token
a2.fun< true >();
^
However the line just above (a.fun< true >()) compiles just fine. Interestingly, if I remove the line template< typename T >, then compilation succeeds. However that line is required for reasons that do not appear from the minimal (not) working example. What is wrong here?
In this context A2 is actually a shorthand for B<T>::A2. Since A2 is a type dependent on the template parameter T, you have to refer to its member templates with an explicit template keyword
a2.template fun< true >();
A is not a dependent type and its member templates can be referred to with "plain" syntax without that extra template keyword.
See 14.2/4 and a similar example there
14.2 Names of template specializations [temp.names]
4 When the name of a member template specialization appears after . or -> in a postfix-expression or after a nested-name-specifier in a qualified-id, and the object expression of the postfix-expression is type-dependent or the nested-name-specifier in the qualified-id refers to a dependent type, but the name is not a member of the current instantiation (14.6.2.1), the member template name must be prefixed by the keyword template.
Otherwise the name is assumed to name a non-template. [ Example:
struct X {
template<std::size_t> X* alloc();
template<std::size_t> static X* adjust();
};
template<class T> void f(T* p) {
T* p1 = p->alloc<200>(); // ill-formed: < means less than
T* p2 = p->template alloc<200>(); // OK: < starts template argument list
T::adjust<100>(); // ill-formed: < means less than
T::template adjust<100>(); // OK: < starts template argument list
}
—end example ]
This doesn't compile:
template<class X> struct A {
template<int I> void f() {}
};
template<class T> void g()
{
A<T> a;
a.f<3>(); // Compilation fails here (Line 18)
}
int main(int argc, char *argv[])
{
g<int>(); // Line 23
}
The compiler (gcc) says:
hhh.cpp: In function 'void g()':
hhh.cpp:18: error: expected primary-expression before ')' token
hhh.cpp: In function 'void g() [with T = int]':
hhh.cpp:23: instantiated from here
hhh.cpp:18: error: invalid use of member (did you forget the '&' ?)
Can anyone explain why this is? Is there a way to get it to work?
Try the following code:
template<class T> void g()
{
A<T> a;
a.template f<3>(); // add `template` keyword here
}
According to C++'03 Standard 14.2/4:
When the name of a member template specialization appears after . or -> in a postfix-expression, or after nested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on a template-parameter (14.6.2), the member template name must be prefixed by the keyword template. Otherwise the name is assumed to name a non-template.
Future C++ Standard seems to be still require this keyword according to draft n2857 14.3/4. Some compilers has special mode that allows to compile original code without errors (Comeau compiles it in so called relaxed mode).
I don't understand why the following code isn't working, why can't I explicitly instantiate the templated function? If I remove that < int > I get a "no matching function call"
#include <iostream>
using namespace std;
template <typename T>
class Xclass
{
public:
template <typename Y>
void xfunc()
{
cout << "Hello";
}
};
template<typename T2>
class Z
{
public:
void x2()
{
Xclass<T2> obj;
obj.xfunc<int>();
}
};
int main() {
Z<int> obj;
obj.x2();
return 0;
}
The error is:
prog.cpp: In member function ‘void Z<T2>::x2()’:
prog.cpp:24:15: error: expected primary-expression before ‘int’
obj.xfunc<int>();
^
prog.cpp:24:15: error: expected ‘;’ before ‘int’
Since the type of obj is a dependent type, you must use the template keyword to tell the compiler it is a template:
Xclass<T2> obj;
obj.template xfunc<int>();
See Where and why do I have to put the "template" and "typename" keywords? for a thorough explanation of when you have to use template.
This doesn't compile:
template<class X> struct A {
template<int I> void f() {}
};
template<class T> void g()
{
A<T> a;
a.f<3>(); // Compilation fails here (Line 18)
}
int main(int argc, char *argv[])
{
g<int>(); // Line 23
}
The compiler (gcc) says:
hhh.cpp: In function 'void g()':
hhh.cpp:18: error: expected primary-expression before ')' token
hhh.cpp: In function 'void g() [with T = int]':
hhh.cpp:23: instantiated from here
hhh.cpp:18: error: invalid use of member (did you forget the '&' ?)
Can anyone explain why this is? Is there a way to get it to work?
Try the following code:
template<class T> void g()
{
A<T> a;
a.template f<3>(); // add `template` keyword here
}
According to C++'03 Standard 14.2/4:
When the name of a member template specialization appears after . or -> in a postfix-expression, or after nested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on a template-parameter (14.6.2), the member template name must be prefixed by the keyword template. Otherwise the name is assumed to name a non-template.
Future C++ Standard seems to be still require this keyword according to draft n2857 14.3/4. Some compilers has special mode that allows to compile original code without errors (Comeau compiles it in so called relaxed mode).