[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#373909: g++-4.0: template function returning nested class fails to compile when defined outside of its class



Package: g++-4.0
Version: 4.0.3-3
Severity: normal


Dear maintainer,

    The following code generates the g++ compiler error:

        14: error: expected constructor, destructor, or type  conversion
            before Outer

    template <typename T>
    class Outer
    {
        public:
            class Inner
            {
            };
        
        Inner get() const;
    };
    
    template <typename T>
    Outer<T>::Inner
    Outer<T>::get() const       // <-- this is line 14
    {
        return Inner();
    }

If the function get() is placed inside the template class Outer, no error is
reported. E.g.,

    template <typename T>
    class Outer
    {
        public:
            class Inner
            {
            };
    
        Inner get() const
        {
            return Inner();
        }
    };

An external definition compiles well if the class Inner is explicitly defined
as a template class (even though no template parameters are required). E.g., 

    template <typename T>
    class Outer
    {
        public:
            template <typename U>
            class Inner
            {
            };
    
        Inner<T> get() const;
    };
    
    
    template <typename T>
    Outer<T>::Inner<T>
    Outer<T>::get() const
    {
        return Inner<T>();
    }


Moreover, the first source, producing a compilation error with g++ was also
offered to the intel C++ compiler, where it compiled flawlessly. Here is a
transcript of the issued commands, illustrating the difference:


arnold@rc-429:~/programming/C++$ g++ -c -Wall demo1.cxx
demo1.cxx:14: error: expected constructor, destructor, or type
conversion before ?Outer?
arnold@rc-429:~/programming/C++$ icc -c -Wall demo1.cxx
arnold@rc-429:~/programming/C++$


My considerations for filing a bug report were:
    * g++ produces a compilation error where icc doesn't;
    * g++'s compilation error is only produced when the template function is
        defined outside of its template class;
    * the reported error message is rather cryptic, given the nature of the
        source, suggesting confusion on the part of the compiler.

Of course, g++ might offer a more rigid implementation of the C++ standard at
this point. However, I couldn't find a clear indication that the external
function implementation violates the standard. Hence the bug report.



-- System Information:
Debian Release: testing/unstable
  APT prefers testing
  APT policy: (500, 'testing')
Architecture: i386 (i686)
Shell:  /bin/sh linked to /bin/bash
Kernel: Linux 2.6.16
Locale: LANG=C, LC_CTYPE=C (charmap=ANSI_X3.4-1968)

Versions of packages g++-4.0 depends on:
ii  gcc-4.0                       4.0.3-3    The GNU C compiler
ii  gcc-4.0-base                  4.0.3-3    The GNU Compiler Collection (base 
ii  libc6                         2.3.6-13   GNU C Library: Shared libraries
ii  libstdc++6-4.0-dev            4.0.3-3    The GNU Standard C++ Library v3 (d

g++-4.0 recommends no packages.

-- no debconf information



Reply to: