Function template call from an other function template

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....)
1605
Äänestä

Muut lukivat myös

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.

Äänestä

Kommentoi aloitusta


Koodari1234 kirjoitti:

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....)

Anonyymi
5000

Ilmoita asiaton sisältö

  • Sisältää materiaalia, joka on loukkaavaa, herjaavaa, rasistista, uhkailevaa tai ahdistelevaa.

  • Materiaali sisältää nimiä, yhteystietoja tai muita henkilökohtaisia tietoja. Julkisuuden henkilöistä, julkisissa viroissa toimivista ihmisistä sekä yritysten vastuuhenkilöistä saa keskustella työhön liittyen. Myös yksityiselämään liittyvistä asioista voi keskustella siltä osin, kuin niistä on julkisesti kerrottu.

  • Sisältää lapsille haitallista tai heiltä kiellettyä materiaalia. Seksuaalinen sisältö on sallittua aiheeseen tarkoitetuilla palstoilla, joiden ikäraja on 18 vuotta.

  • Lainvastaista sisältöä voi olla mm. yksityiselämää loukkaavan tiedon levitys, kiihottaminen kansanryhmää vastaan, laiton uhkaaminen, alaikäisiin liittyvä seksuaalinen sisältö/grooming, petokset, identiteettivarkaudet, kunnianloukkaukset ja tekijänoikeusrikkomukset.

  • Sisältö ei liity palstan tai keskusteluketjun aiheeseen.

  • Viesti ei ole suomea tai ruotsia (pl. International Forums), se sisältää mainontaa, se sisältää tekijänoikeuksin suojattua materiaalia, se on massapostitus tai jokin muu syy.

Jaa keskustelu