Bug 162556 - Compiler error when compiling call to templated function within a templated class
Compiler error when compiling call to templated function within a templated c...
Product: Red Hat Enterprise Linux 3
Classification: Red Hat
Component: gcc (Show other bugs)
All Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
Depends On:
  Show dependency treegraph
Reported: 2005-07-06 06:14 EDT by Michael Doppler
Modified: 2007-11-30 17:07 EST (History)
2 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2005-09-27 05:17:36 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Michael Doppler 2005-07-06 06:14:38 EDT
Description of problem:
The following valid C++ code results in an error message:

class B{
        template <typename T>
        T getValue()
                return T();

class A{
        template <typename T>
        void test(){
                B b;

int main()
        A a;

Version-Release number of selected component (if applicable):

How reproducible:
Compile the code above.

Steps to Reproduce:
1. Paste the code into test.cpp

2. Call the following command in the directory containing the file: 
g++ -o test test.cpp -lstdc++
Actual results:
Error message:
test.cpp: In member function `void A::test()':
test.cpp:15: syntax error before `;' token

Expected results:
Code compiles without an error.

Additional info:
The code compiles fine with g++ 3.4.3-22.1 from RHEL 4.
Comment 2 Alexandre Oliva 2005-09-26 13:35:37 EDT
This is one of those cases in which the C++ grammar is ambiguous, and the parser
in GCC 3.2 wasn't smart enough to tell how to parse the statement because of
limitations in the tools used to create it.  The new parser, introduced in GCC
3.4, was able to overcome this limitation, but it's nearly impossible to
backport the fix.

A syntactic disambiguation, suggested by the standard for situations in which
the postfix-expression before the `.' is template-dependent but accepted in
other cases as well, is to add the `template' keyword before the template-id,
like this:

                b.template getValue<int>();

This code is accepted by GCC 3.2, 3.4, 4.0 and should be accepted by any other
C++ Standard-compliant compilers.
Comment 3 Michael Doppler 2005-09-27 05:17:36 EDT
Thank you for clarifying this issue.

Because resolving the ambiguity works around the problem and a parser backport
is not feasible I set this bug to resolved wontfix.

Note You need to log in before you can comment on or make changes to this bug.