c++/4862: Parse error with templated member function

csk@cs.washington.edu csk@cs.washington.edu
Thu Nov 1 15:18:00 GMT 2001


>Number:         4862
>Category:       c++
>Synopsis:       Parse error with templated member function
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Mon Nov 12 14:16:01 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     csk@cs.washington.edu
>Release:        3.0.2
>Organization:
>Environment:
Redhat 7.2, i686
>Description:
The following code yields a parse error on the line "a.func<T>();":

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

class A
{
public:
    template<typename T>
    void func()
    {
        std::cout << T() << std::endl;
    }
};

template<typename T>
void doit()
{
    A a;
    a.func<T>();
}

int main( int argc, char ** argv )
{
    if( argc > 0 ) {
        doit<int>();
    } else {
        doit<float>();
    }
}
--------------------------------

Surely the definition of "doit" should be legal.  On the other hand, if you erase the "doit" function from the code and replace "main" with this:

----------------------------------
int main( int argc, char ** argv )
{
    if( argc > 0 ) {
        A a;
        a.func<int>();
    } else {
        A a;
        a.func<float>();
    }
}
----------------------------------------------

Everything compiles okay.  This suggests to me that the first version of the code should be legal.
>How-To-Repeat:

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list