Topic: strange namespace-templates error


Author: dsp@bdal.de (=?ISO-8859-1?Q?=22Daniel_Kr=FCgler_=28ne_Spangenberg=29=22?=)
Date: Tue, 10 Aug 2004 05:54:34 GMT
Raw View
Hello Roger Orr,

Roger Orr schrieb:

>hyrosen@mail.com (Hyman Rosen) wrote in message news:<1091730974.381454@=
master.nyc.kbcfp.com>...
> =20
>
>>Alexander T. Bussmann wrote:
>> > template<class C> X<C>& XX:X<C>::operator=3D(const X<C>& rhs) { retu=
rn *this; }
>>
>>     template<class C> XX::X<C>& XX::X<C>::operator=3D(const X<C>& rhs)=
 { return *this; }
>>   =20
>>
>
>Alternatively you can re-open the namespace:
>
>namespace XX
>{
>    template <class C>
>    X<C>& X<C>::operator=3D(const X<C>& rhs) {
>        ....
>    }
>}
>
>Both techniques provide the same end result, but you might prefer the
>second one if several member functions are being defined.
>

You are absolutely right. But there might exist one advantage to define=20
a namespace function as
shown by Hyman and the OP, **iff* the function is a non-member function=20
(which is not in this
example). In that case you can ensure that the definition matches a=20
previous declaration, otherwise
the compiler must complain:

namespace XX {
  template <class C>
  class X;
=20
  template <class C>
  X<C>& assign(X<C>& self, const X<C>& rhs);
}

// The following will not compile, if the above provided declaration is=20
removed or
// changed:
template<class C>
XX::X<C>& XX::assign(X<C>& self, const X<C>& rhs) { return self; }

Greetings from Bremen,

Daniel Kr=FCgler



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: atbussma@rogers.com (Alexander T. Bussmann)
Date: Thu, 5 Aug 2004 18:04:07 GMT
Raw View
I've come across a bit of code that I would expect should compile.  I
also have two methods around it.  I'm hoping that someone more
knowledgable about can shed some light on the subject.

gcc version: 3.3.2 20031022 (Redhat Linux 3.3.2-1)
arch: i586

temp.h
------

namespace XX {
  template<class C>
  class X {
    public:  X& operator=(const X& rhs);
  };
}

temp.cpp
--------

template<class C>
X<C>& XX:X<C>::operator=(const X<C>& rhs) { return *this; }


-- the above code does not compile with the following errors:

temp.cpp:5: error: syntax error before `&' token
temp.cpp:5: error: `C' was not declared in this scope
temp.cpp:5: error: template argument 1 is invalid
temp.cpp:5: error: `X' was not declared in this scope
temp.cpp:5: error: `C' was not declared in this scope
temp.cpp:5: error: `rhs' was not declared in this scope
temp.cpp:6: error: declaration of `operator=' as non-function
temp.cpp:6: error: invalid declarator
temp.cpp:6: error: syntax error before `{' token

Needless to say gcc can't parse the operator= function declaration.
I'm not sure why it would restrict the namespace syntax as I've used
it.  Anybody?

Fix #1 (Preferred Solution)
---------------------------

temp.cpp
--------

using namespace XX;

template<class C>
X<C>& X<C>::operator=(const X<C>& rhs) { return *this; }

The above will compile?  This seems like a rather obscure syntax
limitation!

Fix #2 (Less Preferred Solution)
--------------------------------

test.cpp
--------

namespace XX
{
  template<class C>
  class C {
    public:  X& operator=(const X& rhs) { return *this; }
  };
}

I prefer to seperate definition from implementation.


My question is whether or not the temp.h/temp.cpp example that doesn't
compile, should compile according to the standard, or is this a gcc
bug?

Regards,
Alexander T. Bussmann

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Thu, 5 Aug 2004 18:50:27 GMT
Raw View
Alexander T. Bussmann wrote:
 > template<class C> X<C>& XX:X<C>::operator=(const X<C>& rhs) { return *this; }

     template<class C> XX::X<C>& XX::X<C>::operator=(const X<C>& rhs) { return *this; }

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Bart.van.Ingen.Schenau@ict.nl (Bart van Ingen Schenau)
Date: Fri, 6 Aug 2004 18:24:09 GMT
Raw View
On Thu,  5 Aug 2004 18:50:27 GMT, hyrosen@mail.com (Hyman Rosen)
wrote:

>Alexander T. Bussmann wrote:
> > template<class C> X<C>& XX:X<C>::operator=(const X<C>& rhs) { return *this; }
>
>     template<class C> XX::X<C>& XX::X<C>::operator=(const X<C>& rhs) { return *this; }

or alternatively

namespace XX {
  template <class C>
    X<C>& X<C>::operator=(const X<C>& rhs) { return *this; }
}

Bart v Ingen Schenau

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rogero@howzatt.demon.co.uk (Roger Orr)
Date: Fri, 6 Aug 2004 18:55:13 GMT
Raw View
hyrosen@mail.com (Hyman Rosen) wrote in message news:<1091730974.381454@master.nyc.kbcfp.com>...
> Alexander T. Bussmann wrote:
>  > template<class C> X<C>& XX:X<C>::operator=(const X<C>& rhs) { return *this; }
>
>      template<class C> XX::X<C>& XX::X<C>::operator=(const X<C>& rhs) { return *this; }

Alternatively you can re-open the namespace:

namespace XX
{
    template <class C>
    X<C>& X<C>::operator=(const X<C>& rhs) {
        ....
    }
}

Both techniques provide the same end result, but you might prefer the
second one if several member functions are being defined.

Roger Orr
--
MVP in C++ at www.brainbench.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]