Explicit template instantiation from template function doesn't compile?

Discussion in 'C++' started by Fernando Cuenca, Sep 5, 2004.

  1. Hi,

    I'm trying to explicitly instantiate a template function using the
    following syntax:

    obj.template_func<type>(params);

    It compiles OK when used from a regular function, but it doesn't
    compile when used in a template function. Is there any particular
    reason for this, or is this a compiler bug? I'm using GCC 3.2.3 on
    Debian Linux.

    An example program follows here:

    //---------------------------------------------------------------
    #include <iostream>

    using namespace std;

    class Test
    {
    public:
    template<class T> void DoIt(int i)
    {
    T aT = 0;

    cout << aT << " " << i << endl;
    }
    };

    class Test2
    {
    public:
    template<class T> void CallIt(int i)
    {
    Test aTest;

    //THIS DOESN'T COMPILE: aTest.DoIt<T>(i);
    }

    void CallIt2(int i)
    {
    Test aTest;

    aTest.DoIt<float>(i); // HERE IT COMPILES OK
    }
    };

    int main()
    {
    Test2 aTest;

    //aTest.CallIt(1);
    aTest.CallIt2(2);

    return 0;
    }
    //---------------------------------------------------------------

    In this example, the CallIt function doesn't compile, but CallIt 2,
    which uses the same syntax, but it's not a template itself, compiles
    fine.

    The error message I get is:

    tmplt.cpp: In member function `void Test2::CallIt(int)':
    tmplt.cpp:24: syntax error before `;' token

    Thanks in advance!

    Fernando Cuenca.
    (mail to: fernando_cuenca *at* yahoo *dot* com *dot* ar)
    Fernando Cuenca, Sep 5, 2004
    #1
    1. Advertising

  2. Re: Explicit template instantiation from template function doesn'tcompile?

    Fernando Cuenca wrote:
    > Hi,
    >
    > I'm trying to explicitly instantiate a template function using the
    > following syntax:

    ....
    >
    > In this example, the CallIt function doesn't compile, but CallIt 2,
    > which uses the same syntax, but it's not a template itself, compiles
    > fine.
    >


    FYI - gcc 3.4.0 compiles the snippet without error.
    Gianni Mariani, Sep 5, 2004
    #2
    1. Advertising

  3. "Fernando Cuenca" <> wrote...
    > I'm trying to explicitly instantiate a template function using the
    > following syntax:
    >
    > obj.template_func<type>(params);
    >
    > It compiles OK when used from a regular function, but it doesn't
    > compile when used in a template function. Is there any particular
    > reason for this, or is this a compiler bug? I'm using GCC 3.2.3 on
    > Debian Linux.
    >
    > An example program follows here:
    >
    > //---------------------------------------------------------------
    > #include <iostream>
    >
    > using namespace std;
    >
    > class Test
    > {
    > public:
    > template<class T> void DoIt(int i)
    > {
    > T aT = 0;
    >
    > cout << aT << " " << i << endl;
    > }
    > };
    >
    > class Test2
    > {
    > public:
    > template<class T> void CallIt(int i)
    > {
    > Test aTest;
    >
    > //THIS DOESN'T COMPILE: aTest.DoIt<T>(i);


    Have you tried

    aTest.template DoIt<T>(i);

    ??

    > }
    >
    > void CallIt2(int i)
    > {
    > Test aTest;
    >
    > aTest.DoIt<float>(i); // HERE IT COMPILES OK
    > }
    > };
    >
    > int main()
    > {
    > Test2 aTest;
    >
    > //aTest.CallIt(1);
    > aTest.CallIt2(2);
    >
    > return 0;
    > }
    > //---------------------------------------------------------------
    >
    > In this example, the CallIt function doesn't compile, but CallIt 2,
    > which uses the same syntax, but it's not a template itself, compiles
    > fine.
    >
    > The error message I get is:
    >
    > tmplt.cpp: In member function `void Test2::CallIt(int)':
    > tmplt.cpp:24: syntax error before `;' token
    >
    > Thanks in advance!
    >
    > Fernando Cuenca.
    > (mail to: fernando_cuenca *at* yahoo *dot* com *dot* ar)
    Victor Bazarov, Sep 5, 2004
    #3
  4. "Victor Bazarov" <> wrote in message news:<74J_c.124781$Fg5.23429@attbi_s53>...
    > "Fernando Cuenca" <> wrote...
    > >
    > > //THIS DOESN'T COMPILE: aTest.DoIt<T>(i);

    >
    > Have you tried
    >
    > aTest.template DoIt<T>(i);


    Yes! this compiles fine! Thanks!! Strange syntaxm, though.

    Fernando Cuenca.
    (mail to: fernando_cuenca *at* yahoo *dot* com *dot* ar)
    Fernando Cuenca, Sep 6, 2004
    #4
  5. Re: Explicit template instantiation from template function doesn'tcompile?

    Fernando Cuenca wrote:
    > "Victor Bazarov" <> wrote in message news:<74J_c.124781$Fg5.23429@attbi_s53>...
    >
    >>"Fernando Cuenca" <> wrote...
    >>
    >>> //THIS DOESN'T COMPILE: aTest.DoIt<T>(i);

    >>
    >>Have you tried
    >>
    >> aTest.template DoIt<T>(i);

    >
    >
    > Yes! this compiles fine! Thanks!! Strange syntaxm, though.


    While the code suggested by Victor is portable, it is also a bug in gcc
    3.3.1 that forces you to need it. If I were you, I would upgrade the
    compiler.
    Gianni Mariani, Sep 6, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C. Carbonera
    Replies:
    4
    Views:
    1,340
    C. Carbonera
    Feb 5, 2004
  2. Thomas Maier-Komor
    Replies:
    6
    Views:
    615
    Thomas Maier-Komor
    May 19, 2005
  3. Replies:
    5
    Views:
    487
  4. Replies:
    1
    Views:
    571
    Salt_Peter
    Dec 25, 2006
  5. Noah Roberts
    Replies:
    6
    Views:
    1,152
    Johannes Schaub (litb)
    Feb 2, 2011
Loading...

Share This Page