public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/21515] New: problem with tempalte and tempalte function compilation in a namespace
@ 2005-05-11 18:24 marciso at box43 dot pl
  2005-05-11 18:28 ` [Bug c++/21515] " marciso at box43 dot pl
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: marciso at box43 dot pl @ 2005-05-11 18:24 UTC (permalink / raw)
  To: gcc-bugs

Hi, 

I tried to compile quite simple code: a template and operator<<(ostreami&) for it
(as a template function). The template is in a namespace, but operator<< I'd
like to have in the 'std' namespace. 

Unfortunately, the compiler rejected the code, however other compilers 
(i.e. in other versions) compiled the code without any complainits. 

I'm quite sure that the code is correct, buti, to be honest, I didn't check 
what c++ iso standard is saying about it.

I tested the following code on my on version 3.4.3-20050101 and then build and 
tested on the 3.4.4-20050506 snapshot. The issue occured in both versions.

On the other hand, I compiled successfully the same code on g++ 3.3.5-20050130 
that is installed on other (old&slow) box (gentoo, i586, for details see below). 

Additionally, I tested the same code on 2.95.x on Solaris (SunOS 5.9? - but I don't
remember right now) - it was also compiled.

I didn't check gcc-4.x.

Summary: 

The code (problem.cc):

#include<iostream>
using namespace std;

namespace a {
    template<typename T> class A;
}

template<typename T>
std::ostream & std::operator<< (std::ostream &, a::A<T> const &);

namespace a
{
    template<typename T> class A
    {
        T v;
    public:
        A(T _v) : v(_v) {};
        friend std::ostream & std::operator<< <>(std::ostream &, A<T> const &);
    };
}


template<typename T>
std::ostream & std::operator<<(std::ostream & o, a::A<T> const & aa)
{
    return o << aa.v ;
}

int main()
{
    a::A<float> o(16.0);

    cerr << o << endl;
}


Command: g++ problem.cc 

Output: 

problem.cc: In instantiation of `a::A<float>':
problem.cc:32:   instantiated from here
problem.cc:19: error: template-id `operator<< <>' for `std::basic_ostream<char,
std::char_traits<char> >& std::operator<<(std::basic_ostream<char,
std::char_traits<char> >&, const a::A<float>&)' does not match any template
declaration
problem.cc: In function `std::ostream& std::operator<<(std::ostream&, const
a::A<T>&) [with T = float]':
problem.cc:34:   instantiated from here
problem.cc:16: error: `float a::A<float>::v' is private
problem.cc:27: error: within this context


Compilation failed on 3.4.3-20050101 and 3.4.4-20050506 on my box:

Release:       3.4.4 20050506 (prerelease)
Environment:
System: Linux baki 2.6.12-rc1baki #5 Sat Mar 26 09:01:44 UTC 2005 i686 AMD
Athlon(tm) XP 2000+ AuthenticAMD GNU/Linux
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with:  :
(reconfigured) ./configure --prefix=/home/marciso/gcc-tmp-installatio
--enable-languages=c++
                                                 
          
Release:3.4.3-20050110 (Gentoo Linux 3.4.3.20050110-r2, ssp-3.4.3.20050110-0,
pie-8.7.7)
Environment:
System: Linux baki 2.6.12-rc1baki #5 Sat Mar 26 09:01:44 UTC 2005 i686 AMD
Athlon(tm) XP 2000+ AuthenticAMD GNU/Linux
Architecture: i686

host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: /var/tmp/portage/gcc-3.4.3.20050110-r2/work/gcc-3.4.3/configure
--enable-version-specific-runtime-libs --prefix=/usr
--bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.4.3-20050110
--includedir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.3-20050110/include
--datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.3-20050110
--mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.3-20050110/man
--infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.3-20050110/info
--with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.3-20050110/include/g++-v3
--host=i686-pc-linux-gnu --disable-altivec --enable-nls
--without-included-gettext --with-system-zlib --disable-checking
--disable-werror --disable-libunwind-exceptions --disable-multilib
--enable-java-awt=gtk --enable-languages=c,c++,objc,java,f77 --enable-shared
--enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu



Compilation passed on 3.3.5-20050130:

Release:       3.3.5-20050130 (Gentoo Linux 3.3.5.20050130-r1,
ssp-3.3.5.20050130-1, pie-8.7.7.1)
Environment:
System: Linux gateway 2.6.10-hardened-r3bramka #2 Sun Feb 20 12:58:22 CET 2005
i586 GNU/Linux
Architecture: i586

host: i586-pc-linux-gnu
build: i586-pc-linux-gnu
target: i586-pc-linux-gnu
configured with:
/var/tmp/portage/gcc-3.3.5.20050130-r1/work/gcc-3.3.5/configure
--enable-version-specific-runtime-libs --prefix=/usr
--bindir=/usr/i586-pc-linux-gnu/gcc-bin/3.3.5-20050130
--includedir=/usr/lib/gcc-lib/i586-pc-linux-gnu/3.3.5-20050130/include
--datadir=/usr/share/gcc-data/i586-pc-linux-gnu/3.3.5-20050130
--mandir=/usr/share/gcc-data/i586-pc-linux-gnu/3.3.5-20050130/man
--infodir=/usr/share/gcc-data/i586-pc-linux-gnu/3.3.5-20050130/info
--with-gxx-include-dir=/usr/lib/gcc-lib/i586-pc-linux-gnu/3.3.5-20050130/include/g++-v3
--host=i586-pc-linux-gnu --disable-altivec --enable-nls
--without-included-gettext --with-system-zlib --disable-checking
--disable-werror --disable-libunwind-exceptions --disable-multilib
--disable-libgcj --enable-languages=c,c++,f77 --enable-shared
--enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu



Cheers
Marcin.

-- 
           Summary: problem with tempalte and tempalte function compilation
                    in a namespace
           Product: gcc
           Version: 3.3.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: marciso at box43 dot pl
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/21515] problem with tempalte and tempalte function compilation in a namespace
  2005-05-11 18:24 [Bug c++/21515] New: problem with tempalte and tempalte function compilation in a namespace marciso at box43 dot pl
@ 2005-05-11 18:28 ` marciso at box43 dot pl
  2005-05-11 18:31 ` marciso at box43 dot pl
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: marciso at box43 dot pl @ 2005-05-11 18:28 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From marciso at box43 dot pl  2005-05-11 18:28 -------
Created an attachment (id=8861)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=8861&action=view)
*.ii

g++ -save-temps  problem.cc

-- 


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/21515] problem with tempalte and tempalte function compilation in a namespace
  2005-05-11 18:24 [Bug c++/21515] New: problem with tempalte and tempalte function compilation in a namespace marciso at box43 dot pl
  2005-05-11 18:28 ` [Bug c++/21515] " marciso at box43 dot pl
@ 2005-05-11 18:31 ` marciso at box43 dot pl
  2005-05-12 17:45 ` [Bug c++/21515] problem with template and template " bangerth at dealii dot org
  2005-05-12 17:49 ` bangerth at dealii dot org
  3 siblings, 0 replies; 5+ messages in thread
From: marciso at box43 dot pl @ 2005-05-11 18:31 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |marciso at box43 dot pl
            Version|3.3.4                       |3.4.3


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/21515] problem with template and template function compilation in a namespace
  2005-05-11 18:24 [Bug c++/21515] New: problem with tempalte and tempalte function compilation in a namespace marciso at box43 dot pl
  2005-05-11 18:28 ` [Bug c++/21515] " marciso at box43 dot pl
  2005-05-11 18:31 ` marciso at box43 dot pl
@ 2005-05-12 17:45 ` bangerth at dealii dot org
  2005-05-12 17:49 ` bangerth at dealii dot org
  3 siblings, 0 replies; 5+ messages in thread
From: bangerth at dealii dot org @ 2005-05-12 17:45 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|problem with tempalte and   |problem with template and
                   |tempalte function           |template function
                   |compilation in a namespace  |compilation in a namespace


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/21515] problem with template and template function compilation in a namespace
  2005-05-11 18:24 [Bug c++/21515] New: problem with tempalte and tempalte function compilation in a namespace marciso at box43 dot pl
                   ` (2 preceding siblings ...)
  2005-05-12 17:45 ` [Bug c++/21515] problem with template and template " bangerth at dealii dot org
@ 2005-05-12 17:49 ` bangerth at dealii dot org
  3 siblings, 0 replies; 5+ messages in thread
From: bangerth at dealii dot org @ 2005-05-12 17:49 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2005-05-12 17:49 -------
You can't do this: 
  template<typename T> 
  std::ostream & std::operator<< (std::ostream &, a::A<T> const &); 
If you want to overload something in namespace std, you have to open that 
namespace, put the declaration in it, and close it again. If you do this 
everything is fine, i.e. like so: 
 
namespace std 
{ 
  template<typename T> 
  ostream & operator<< (ostream &, a::A<T> const &); 
} 
 
The fact that we don't reject the declaration is a bug in gcc in itself, 
for which I'll open a PR in a minute. 
 
W. 

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


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-05-12 17:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-11 18:24 [Bug c++/21515] New: problem with tempalte and tempalte function compilation in a namespace marciso at box43 dot pl
2005-05-11 18:28 ` [Bug c++/21515] " marciso at box43 dot pl
2005-05-11 18:31 ` marciso at box43 dot pl
2005-05-12 17:45 ` [Bug c++/21515] problem with template and template " bangerth at dealii dot org
2005-05-12 17:49 ` bangerth at dealii dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).