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.
Related
Why does using static template class member as friend template function default parameter give me compile error in c++? How to slove?
Here is the code:
#include <iostream>
template<typename T>
void func(T n);
template<typename T>
class MyClass
{
private:
static T statTemp;
public:
friend void func<>(T n);
};
template<typename T>
T MyClass<T>::statTemp(1);
template<typename T>
void func(T n = MyClass<T>::statTemp)
{
std::cout << n << std::endl;
}
int main()
{
func<int>();
}
On compile:
g++ -std=c++11 main.cpp
error: redeclaration of 'template<class T> void func(T)' may not have default arguments [-fpermissive]
void func(T n = MyClass<T>::statTemp)
^~~~
In function 'int main()':
error: no matching function for call to 'func<int>()'
func<int>();
^
note: candidate: 'template<class T> void func(T)'
void func(T n = MyClass<T>::statTemp)
^~~~
note: template argument deduction/substitution failed:
note: candidate expects 1 argument, 0 provided
func<int>();
^
visual studio 2017
C2672 "func": No matching overloaded function found.
The language does not allow default arguments for template functions to be added in later declarations of a function in the same scope.
The C++17 standard draft n4659 states :
11.3.6 Default arguments [dcl.fct.default]
...
4 For non-template functions, default arguments can be added in later declarations of a function in the same scope.
Since func is a template function, it is not permitted to add default arguments in later declarations of a func in the same scope.
So GCC correctly rejects this as such:
error: redeclaration of 'template<class T> void func(T)' may not have default arguments [-fpermissive]
void func(T n = MyClass<T>::statTemp)
I got your program to compile, however, I am honestly not sure what it is I really did. I can only take my intuition through it and do the best I can to explain what I just did:
#include <iostream>
template<typename T>
void func();
template<typename T>
class MyClass
{
private:
static T statTemp;
public:
friend void func<T>();
};
template<typename T>
T MyClass<T>::statTemp(1);
template<typename T>
void func()
{
T n = MyClass<T>::statTemp;
std::cout << n << std::endl;
}
int main()
{
func<int>();
}
First, I noticed your original funct(T n) you had declared this at the top of the file and it expected a T n to be passed into the function. In main, you do no such thing. So when you call func(T n) and pass nothing into the function, the compiler gets angry since it expects something to be passed in. Maybe what you can do, is overload the function in which calls another func(T n) where you pass it T n = MyClass<T>::statTemp;
Lastly, your friend void func<>() had no template type so the compiler was angry with that as well.
I hope that helps.
This question already has answers here:
C++ template compilation error: expected primary-expression before ‘>’ token [duplicate]
(1 answer)
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 4 years ago.
Is it possible to pass object of a class with templated method as a template parameter under GCC? All search I conducted pointed me to questions regarding templated class, which is not applicable in my case.
Sample code:
#include <iostream>
class Foo
{
public:
template<typename T>
void print(const T& arg) const
{
std::cout << arg << std::endl;
}
};
template<typename A>
void print(const A& printer)
{
printer.print<int>(1);
}
int main()
{
Foo foo;
print<Foo>(foo);
return 0;
}
Compiling under MinGW.org GCC-6.3.0-1 I get the following results:
tmpl.cpp: In function 'void print(const A&)':
tmpl.cpp:16:19: error: expected primary-expression before 'int'
printer.print<int>(1);
^~~
tmpl.cpp:16:19: error: expected ';' before 'int'
VisualStudio compiles the same code cleanly, application works as expected. If I make print function not templated, so that is explicitly takes object of Foo it works as expected both compilers.
You need to use template keyword to tell that print is a template.
Inside a template definition, template can be used to declare that a dependent name is a template.
e.g.
template<typename A>
void print(const A& printer)
{
printer.template print<int>(1);
// ~~~~~~~~
}
print is a dependent name, it depends on the template parameter A. Before the template gets instantiated the compiler knows nothing about A; so you need to add the template keyword to tell the compiler that print is a template, then the < after the name print is valid; otherwise < will be treated as the operator< then cause the error.
You need the template keyword in order to specify that print() is a template, like this:
printer.template print<int>(1);
This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 6 years ago.
The following code (also on ideone):
// A type templated for different function call signatures.
template <typename Signature> class Used;
template <typename T_Ret, typename ...T_Args>
class Used<T_Ret(T_Args...)> {
public:
// A static method for a specific type.
template <typename T>
static void specific() { }
};
// Some class using the above.
template <typename T>
class User {
public:
// A method that must call the specific function of used.
template <typename T_Ret, typename ...T_Args>
void method() {
using It = Used<T_Ret(T_Args...)>;
using Me = T;
// error: expected primary-expression before '>' token
It::specific<Me>();
}
};
int main() {
User<int> user;
user.method<void, int>();
}
gives the following error (at least using GCC):
test.cpp: In member function 'void User<T>::method()':
test.cpp:20:18: error: expected primary-expression before '>' token
It::specific<Me>();
^
And I have no idea why... The error does not occur if the template parameters are removed from the Used class.
Am I doing something wrong (i.e., missing a template or typename keyword somewhere)? Is this a bug in GCC? Is there a workaround?
Because specific is a dependent name you need a template keyword:
It::template specific<Me>();
//--^^^^^^^^
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 ]
Sorry could not find a better title for my question. Basically what I noticed was the following compiles fine:
#include <vector>
void foo();
int main () {
foo();
return 0;
}
namespace{
struct point {
double x, y;
};
}
void foo(){
std::vector<point> p;
}
whereas compiler complains about the following:
#include <vector>
void foo();
int main () {
foo();
return 0;
}
void foo(){
struct point {
double x, y;
};
std::vector<point> p;
}
// g++ output:
a.cpp: In function ‘void foo()’:
a.cpp:14: error: template argument for ‘template<class _Tp> class std::allocator’ uses local type ‘foo()::point’
a.cpp:14: error: trying to instantiate ‘template<class _Tp> class std::allocator’
a.cpp:14: error: template argument 2 is invalid
a.cpp:14: error: invalid type in declaration before ‘;’ token
I want to know what is wrong with the second approach? Isn't struct point completely defined at the point of creating a new std::vector<point> object?
This is due to a limitation in C++03 (now lifted in C++11), that simply forbids the use of local types (i.e. types defined in function bodies) as template arguments.