The following code
template<typename T>
void f();
template<typename T>
class A{
class B{
void f(){
f<T>();
}
};
};
does not compile, and gives the following error
<source>:10:15: error: expected primary-expression before '>' token
10 | f<T>();
What is the correct way to refer to T in the inner class?
Your problem is that f is not the name of a template on that line. It is the name of the method.
::f<T>(t);
Try that.
Related
I have the following code:
template <class T>
class Outer
{
public:
Outer();
template <class U>
void templateFunc()
{
}
private:
class Inner
{
public:
Inner(Outer& outer)
{
outer.templateFunc<int>();
Outer* outer_ptr = &outer;
[outer_ptr]()
{
outer_ptr->templateFunc<int>();
}();
}
};
Inner m_inner;
};
template <class T>
Outer<T>::Outer()
: m_inner(*this)
{
}
int main()
{
Outer<double> outer;
}
As you can see, there is a template class that contains a nested class, which in constructor calls some template method of its enclosing class. AFAIK, even though enclosing class is a template class - for the nested class it is a non-dependent name, so calling its template method without template should not be a problem. The problem happens when I define a lambda inside nested class' constructor, capture pointer to outer class, and try to call the same template method - g++7.2 gives the following compilation error:
prog.cc: In lambda function:
prog.cc:22:41: error: expected primary-expression before 'int'
outer_ptr->templateFunc<int>();
^~~
prog.cc:22:41: error: expected ';' before 'int'
However, g++-5.4 and g++-6.3 compile this code just fine. So it seems that g++-7.2 treats the outer_ptr's type inside the lambda as a dependent name - and I cannot understand why. Can someone explain this to me?
Yes, this is a gcc regression. Filed as 82980.
Here's a reduced example:
template <class T>
struct Outer
{
template <class U>
void f();
void bar(Outer outer) {
[outer](){ outer.f<int>(); };
}
};
int main() { }
outer.f is member access for the current instantiation, so that expression shouldn't count as type dependent, so you shouldn't need to provide the template keyword.
This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 8 years ago.
I have to following example code. In this code I have two template classes each with a template function. One of the classes has an object of the other class and in the template function it calls the template of the other class.
template<typename T>
class A{
public:
A(T a): a(a){}
template< typename V> V foo(){
return this->a;
}
private:
T a;
};
template<typename T>
class B{
public:
B( A<T> a): a(a){}
template<typename V>V foo2(){
return this->a.foo<V>();
}
private:
A<T> a;
};
int
main()
{
A<int> a(5);
double aTest = a.foo<double>();
B<int> b(a);
double c = b.foo2< double >();
}
I need to provide the template element after the function call, because automatic type deduction does not work for functions, were only the return type depends on the template parameter. This works for aTest. But when I add the next two lines I get the following compiler error:
build/main.cpp: In member function 'V B<T>::foo2()':
build/main.cpp:32:23: error: expected primary-expression before '>' token
return this->a.foo<V>();
^
build/main.cpp:32:25: error: expected primary-expression before ')' token
return this->a.foo<V>();
You need to use:
template<typename V>V foo2(){
return this->a.template foo<V>();
}
To find gory details of why you need to use template in the call to a.foo, please take a look at this SO answer: https://stackoverflow.com/a/613132/434551
I'm getting an error when compiling a code that can be trivialized as follows:
#include<iostream>
template <class T>
class A
{
protected:
T protectedValue;
template<class TT>
class insideClass
{
public:
TT insideClassValue;
};
};
template<class T>
class B : public A<T>
{
public:
void print(T t)
{
insideClass<T> ic; // <-- the problem should be here
ic.insideClassValue = t;
std::cout << ic.indideClassValue << std::endl;
};
};
int main()
{
double v = 2.;
B<double> b;
b.print(v);
return 0;
};
The compiler (g++) gives the following error:
main.C: In member function ‘void B<T>::printA()’:
main.C:23:4: error: ‘insideClass’ was not declared in this scope
main.C:23:17: error: expected primary-expression before ‘>’ token
main.C:23:19: error: ‘ic’ was not declared in this scope
I figured out that if class A is not a template class, the compilation will not give any error.
I do not understand why making class A a template class results in the described error.
Any idea about the reasons and how to fix the problem?
Without qualification insideClass is a non-dependent name which is looked up during phase 1 look-up. Since the definition of the base depending on a template argument isn't known, names from the base class are ignored and the name isn't found. Qualification and possibly adding typename in a strategic place should solve the problem (thanks to remyabel for the notation):
typename A<T>::template insideClass<T> ic;
The template keyword is needed to indicate that what's coming is a template and the typename is needed to indicate that happens to be a type. Getting the correct spelling of what a dependent name is supposed to be is sometimes not entirely straight forward. A SSCCE showing the problem is here and the solution is here.
Something like this:
typedef typename A<T>::template insideClass<T> ic;
public:
void print(T t)
{
ic ic;
ic.insideClassValue = t;
std::cout << ic.insideClassValue << std::endl;
};
I'm experiencing a problem in some code I've been working on. Here is the most simplified version of it I could create:
template <class A>
class Outer {
public:
template <class B>
class Inner {
public:
template <class C>
void foo(Outer<C>::Inner<C> innerC) { }
};
Inner<A> inner;
};
class X {};
class Y {};
int main() {
Outer<X> outerX;
Outer<Y> outerY;
outerX.foo<Y>(outerY.inner);
}
The error is:
error: expected primary-expression before ">" token
and is triggered at compiletime at the declaration of void foo. What is incorrect about my code that is making this happen?
In words, what I am trying to do is have the nested class be able to take in a nested class with any template type - but of course the nested class's template type depends on the outer class's template type, so I use the :: syntax, but that gives an error.
I understand that what I'm trying to do here might not be a good practice, but the purpose of this question is to understand template syntax better.
There is no conversion from 1 to Inner<C>. Is that an error in your reduced test case, or is it supposed to be:
template <class C>
void foo(C innerC) { }
Update: After the code was fixed, it can be seen that the problem is the lack of template before Inner<C>. Otherwise the compiler will assume that Inner is a value.
template <class C>
void foo(Outer<C>::template Inner<C> innerC) { }
I'm trying to compile some Microsoft Visual C++ code using g++. Now I've come across a compiler error which I really can't understand. The (simplified) code looks like this:
template<int X> struct A {
template<class Ret> static Ret call() {
return 0;
}
};
template<int X> struct B : A<X> {
int f() {
return A<X>::call<int>();
}
};
When I try to compile this with g++ (version 4.4.5), I get the following error:
main.cpp: In member function int B<X>::f():
main.cpp:16: error: expected primary-expression before int
main.cpp:16: error: expected ; before int
main.cpp:16: error: expected unqualified-id before > token
If I remove the template type (Ret) from method A::call, the code compiles just fine. Can anybody see whats wrong here?
Thanks!
You need the template keyword:
return A<X>::template call<int>();
call is a dependent name, meaning that its signification depends on a template parameter, which is not known when the compiler process f(). You need to indicate that call is a function template by prefixing it with the template keyword.
The same thing happens when you try to access a nested type: you need to add the typename keyword to indicate that the name denotes a type:
template <typename T>
struct A { typedef int type; };
template <typename T>
void f()
{
typename A<T>::type i = 0; // notice "typename" here
}
And sometimes you even need to mix both:
template <typename T>
struct A
{
template <typename U>
struct inner { };
};
template <typename T>
void f()
{
typename A<T>::template inner<int> var;
}
The use of these two keywords is thoroughly explained in the answers to this question: Where and why do I have to put the “template” and “typename” keywords? (thanks to #Björn Pollex for finding the link).
A is a template, and without knowing X the compiler can't determine the contents of A<X>. Especially it doesn't know that call will end up being a template.
To tell that to the compiler you have to use the template keyword:
template<int X> struct B : A<X> {
int f() {
return A<X>::template call<int>();
}
};
You have to specify that the function you're calling is a template, as it's part of a template class. The compiler is not aware that any given A<X> has a template function named call, and therefore you need to help it.
template<int X> struct B : A<X> {
int f() {
return A<X>::template call<int>();
}
};