Topic: Function template call from an other function template
A member function template is tried to be called from an other separate function template.
-> Compiler says something like this: "parse error before `>' ".
Is it possible to make this working somehow?
Would "typename" or some other kinds of words help somehow?
(E.g. I needed to add "typeName" before "aaT::someType x;" to compile that part of code successfully.)
My test code
The problem is on line "varAa.foo();" (it do not compile).
template
void function1 ()
{
typename aaT::someType x;
aaT varAa;
varAa.foo();
}
class myClass {
public:
typedef int someType;
template
void foo () {}
};
int main() {
function1();
}
And, even if I do not create any instances of that function template (even if I do not call it at all), it still complains about the same line. E.g. if I do not execute anything at all in the main program, then it still complains about the mentioned line ("varAa.foo();").
int main() {
// comment this line out:
//function1();
}
It seems that the compiler wants the critical line of code ("varAa.foo();") to be compiled "too early". The compiler should "wait" for the template instantiation (or something like that?). I should somehow ask compiler to wait for the instantiation of "template void function1()"? The member function template "foo" will be found from "varAa" (from myClass) just a little bit later!
And of course, if I execute foo straight from myClass object (and remove the critical line of code "varAa.foo();"), then the function works normally (this should work ok):
int main() {
myClass myObj;
myObj.foo();
}
One more addition, if I call the member function template from a normal function then it works ok. (In the following code I use a normal function called "function2", and it works ok. -> I can call "varAa.foo();" from it):
class myClass {
public:
typedef int someType;
template
void foo () {}
};
void function2 ()
{
myClass::someType x;
myClass varAa;
varAa.foo();
}
int main() {
function2();
}
But, I need to call a member function template from a function template (instead of from a normal function), as it is described in the first code example.
Sorry, this question was in english. (Olen kirjoittanut tämän jo aiemmin englanniksi ja täällä ehkä osataan englantiakin, niin ei ehkä tarvitse suomeksi laittaa....)
A member function template is tried to be called from an other separate function template.
-> Compiler says something like this: "parse error before `>' ".
Is it possible to make this working somehow?
Would "typename" or some other kinds of words help somehow?
(E.g. I needed to add "typeName" before "aaT::someType x;" to compile that part of code successfully.)
My test code
The problem is on line "varAa.foo();" (it do not compile).
template
void function1 ()
{
typename aaT::someType x;
aaT varAa;
varAa.foo();
}
class myClass {
public:
typedef int someType;
template
void foo () {}
};
int main() {
function1();
}
And, even if I do not create any instances of that function template (even if I do not call it at all), it still complains about the same line. E.g. if I do not execute anything at all in the main program, then it still complains about the mentioned line ("varAa.foo();").
int main() {
// comment this line out:
//function1();
}
It seems that the compiler wants the critical line of code ("varAa.foo();") to be compiled "too early". The compiler should "wait" for the template instantiation (or something like that?). I should somehow ask compiler to wait for the instantiation of "template void function1()"? The member function template "foo" will be found from "varAa" (from myClass) just a little bit later!
And of course, if I execute foo straight from myClass object (and remove the critical line of code "varAa.foo();"), then the function works normally (this should work ok):
int main() {
myClass myObj;
myObj.foo();
}
One more addition, if I call the member function template from a normal function then it works ok. (In the following code I use a normal function called "function2", and it works ok. -> I can call "varAa.foo();" from it):
class myClass {
public:
typedef int someType;
template
void foo () {}
};
void function2 ()
{
myClass::someType x;
myClass varAa;
varAa.foo();
}
int main() {
function2();
}
But, I need to call a member function template from a function template (instead of from a normal function), as it is described in the first code example.
Sorry, this question was in english. (Olen kirjoittanut tämän jo aiemmin englanniksi ja täällä ehkä osataan englantiakin, niin ei ehkä tarvitse suomeksi laittaa....)
Vastaukset 1
Vanhimmat
I got an answer from a code expert.
This helps:
varAa.template foo();
C standard says that without the keyword "template", compiler assumes that the name is a non-template.