[Bug c++/36151] gcc fails to find template specializations within namespace

juergen dot wallner at philips dot com gcc-bugzilla@gcc.gnu.org
Wed May 14 09:24:00 GMT 2008



------- Comment #2 from juergen dot wallner at philips dot com  2008-05-14 09:23 -------
1.) I've been unable to find an explanation of this behaviour in ISO/IEC 14882
or other literature like Stroustrup - The C++ Programming Language.

Please explain and cite reference: How can a unique name from an enclosing
scope be "hidden" by a namespace?

2.) It also doesen't work the other way around (template classes in namespace):

namespace {
        class X
        {
        public:
                void foo() {}
        };

        template<class T> class A
        {
        public:
                T& operator*() { return *m; }
                T *m;
        };

        template<class T> class B
        {
        public:
                T& operator*() { return *m; }
                T *m;
        };
}

template<class T> void bar( T& v )      { v.foo(); }
template<class T> void bar( B<T>& x )   { bar( *x ); }
template<class T> void bar( A<T>& x )   { bar( *x ); }

int main()
{
        B< A<X> > m1; 
        bar( m1 );      // ERROR if class X is in ANY namespace, otherwise OK

        A< B<X> > m2;   
        bar( m2 );      // OK
}
--
3.) A similar and more practical example (working on 4.0.1 but not 4.2.1.):

// io_int.h
void Write( int x )
{
}

// io_vector.h
#include <vector>
template<class T> void Write(const std::vector<T> &x)
{
        for(typename std::vector<T>::const_iterator i=x.begin() ; i!=x.end() ;
++i)
                Write(*i);
}

// io_list.h
#include <list>
template<class T> void Write(const std::list<T> &x)
{
        for(typename std::list<T>::const_iterator i=x.begin() ; i!=x.end() ;
++i)
                Write(*i);
}

// main.cc
int main(int argc, char *argv[])
{
        std::vector< std::list<int> > o;

        Write(o);
}
--
If we were to implement IO functionality for stl containers, someone would have
to forward declare Write functions of ALL containers in every single io_xxx.h,
or the user of this functionality would have to forward declare all write
templates he intends to use prior to including any of the io_xxx.h - all
because  the stl containers are inside the std-namespace (which should be
completely irrelevant)


-- 

juergen dot wallner at philips dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


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



More information about the Gcc-bugs mailing list