bug-gplusplus
[Top][All Lists]
Advanced

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

namespaces and templates


From: Steffen Jahn
Subject: namespaces and templates
Date: 17 Apr 2001 10:22:13 GMT

Hi,

there are problem with namespaces and templates in g++. Below two samples
for such problems. To work around such problems, you have to wrap the 
namespaces scope (braces) around your implementation. 

regards
Steffen

Below two cross postings.

Hi,

I wrote some templates and want to assign certain name spaces now. There is
no problem to put the code into the scope of the namespace (below the
template X in the name spaces foo and bar).

  namespace foo {
    template <class T> class X
    { public: X() {} } ;
  } ;

  namespace bar {
    template <class T> class X
    {  public: X() {} } ;
  } ;

But if I try to implement the class "outside" the namespace, I'm lost. E.g.

  namespace foo { class X ; } ;
  namespace bar { class X ; } ;

  template <class T> class foo::X
  { public: X() {} } ;

  template <class T> class bar::X
  { public: X() {} } ;

results in

  9: invalid redeclaration of template <class T> bar::X
  9: redefinition of `class foo::X<T>'
  6: previous definition here

Or more straight forward:

  namespace foo { template <class T> class X ; } ;
  namespace bar { template <class T> class X ; } ;

  template <class T> class foo::X
  { public: X() {} } ;

  template <class T> class bar::X
  { public: X() {} } ;

results in

  6: ANSI C++ forbids declaration `X' with no type
  9: invalid redeclaration of template <class T> bar::X<T>
  9: redefinition of `class foo::X<T><T>'
  6: previous definition here
  9: ANSI C++ forbids declaration `X' with no type

What's wrong?

TIA
Steffen

Hi all,

I use heavily templates and namespaces in my source code. Now I encountered
a problem, which can be reduced to the piece of code below:

#include <iostream>

namespace N1
{
  template <class T> class X
  {
  public:
    friend void f<T>(X<T>*) ;
    T t ;
  } ;
  template <class T> void f(X<T>*) {}
} ;

#ifdef FOO
class B
{ public: N1::X<int> x ; } ;
#endif

namespace N2 { class A ; } ;

class N2::A
{
public:
  N1::X<int> x ;
} ;

void main() { N2::A a ; a.x.t = 10 ; cout << a.x.t << endl ; }

I know, it looks a little bit weird. In fact, there are two name spaces
and a template. If you compile it, you get the following error message:

foo.cc: In instantiation of `N1::X<int>':
foo.cc:25:   instantiated from here
foo.cc:9: `f' was not declared in this scope

This is a little bit strange for me. But very surprising is the result
if you define FOO. In this case, the compiler does not complain and the
running binary returns the expected output (10).

Playing around with this piece of code, the compiler does not complain
if you omit one of the namespaces or the template. To work around the
the problem, you can simply define the class A within the scope (braces)
of the namespace N2. This seems often to fix such problems with g++.

What I'm now intereted in, is it a compiler bug or does it not confirm
to C++?

TIA
Steffen

btw:

% g++ -v
Reading specs from .../gcc-lib/alphaev56-dec-osf4.0d/2.95.2/specs
gcc version 2.95.2 19991024 (release)

-- 
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]