This code does not compile:

=====CUT=====
template <typename T>
struct A
{
};
template <>
struct A<int>
{
    typedef int Type;
};


template <typename T>
struct B
{
    typedef typename A<T>::Type Type;
};


template <typename T>
inline typename B<T>::Type f(T & me)
{
    return 0;
}

template <typename T>
inline typename B<T const>::Type f(T const & me)
{
    return 0;
}
     

int main()
{
    int x = 0;
    f(x);

    return 0;
}

=====CUT=====

This is the error message:
%g++ test.cpp
test.cpp: In instantiation of `B<const int>':
test.cpp:34:   instantiated from here
test.cpp:14: error: no type named `Type' in `struct A<const int>'

Only the first overload of function f is used. Since the compiler does not 
instantiates the second overload of function f, there is no need for looking up
(and not finding) typename B<const int>::Type.

Note that no error message occur, if we leave out the indirection through 
struct B, that is the following code compiles well:

====CUT====
template <typename T>
struct A
{
};
template <>
struct A<int>
{
    typedef int Type;
};

template <typename T>
inline typename A<T>::Type f(T & me)
{
    return 0;
}

template <typename T>
inline typename A<T const>::Type f(T const & me)
{
    return 0;
}
     

int main()
{
    int x = 0;
    f(x);

    return 0;
}
====CUT====


compiler option -v returns this:

Reading specs from /usr/lib/gcc/i486-linux/3.4.4/specs
Configured with: ../src/configure -v --enable-
languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr --
libexecdir=/usr/lib --with-gxx-include-dir=/usr/include/c++/3.4 --enable-
shared --with-system-zlib --enable-nls --without-included-gettext --program-
suffix=-3.4 --enable-__cxa_atexit --enable-libstdcxx-allocator=mt --enable-
clocale=gnu --enable-libstdcxx-debug --enable-java-gc=boehm --enable-java-
awt=gtk --disable-werror i486-linux
Thread model: posix
gcc version 3.4.4 20041218 (prerelease) (Debian 3.4.3-6)

-- 
           Summary: Dependend return value of function template is looked up
                    even if function is never instantiated
           Product: gcc
           Version: 3.4.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: schlauerhamster at yahoo dot de
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20627

Reply via email to