public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/19610] New: default constructor not called for static template member of template class
@ 2005-01-24 20:22 jamesp at trdlnk dot com
  2005-01-24 20:38 ` [Bug c++/19610] " pinskia at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: jamesp at trdlnk dot com @ 2005-01-24 20:22 UTC (permalink / raw)
  To: gcc-bugs

I've run into a problem where a static template member of a template class is
not being created properly.

--- Begin Sample Code ---
#include <iostream>
using namespace std;

template <typename T>
class A
{
public:
    A() { cout << "A created" << endl; }
};

template <typename T>
class AA
{
public:
    AA( int i = 0 ) { cout << "AA " << i << " created" << endl; }
};

template <typename T>
class B
{
public:
    static A<T> a;
    static AA<T> aa;
    static AA<T> aa2;
};

template <> A<char> B<char>::a;
template <> AA<char> B<char>::aa;
template <> AA<char> B<char>::aa2(1);

int main() { }
--- End Sample Code ---

When I run the above sample, I get the following:
%g++ -Wall test.cc
%./a.out           
AA 1 created
%

I expected all three of class B's static template members to be constructed. It
seems that the problem occurs whether it should have called a regular default
constructor or a conversion constructor with a default parameter. The fact that
B is a template class also seems to play some role in this. I tried another
example where B was a non-template class and everything constructed properly.

-- 
           Summary: default constructor not called for static template
                    member of template class
           Product: gcc
           Version: 3.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jamesp at trdlnk dot com
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: i686-pc-linux-gnu


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


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

* [Bug c++/19610] default constructor not called for static template member of template class
  2005-01-24 20:22 [Bug c++/19610] New: default constructor not called for static template member of template class jamesp at trdlnk dot com
@ 2005-01-24 20:38 ` pinskia at gcc dot gnu dot org
  2005-01-24 21:19 ` jamesp at trdlnk dot com
  2005-01-26  4:16 ` bangerth at dealii dot org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-01-24 20:38 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-01-24 20:38 -------
I think this is invalid because you are just specializing them and nothing else (note the aa2 can be done 
the non specialized way too but I showed the specialized way):
// declare space for them
template<typename T> A<T> B<T>::a;
template<typename T> AA<T> B<T>::aa;
//specialize B<char>::aa2
template<> AA<char> B<char>::aa2(1);

//instantiate them
template A<char> B<char>::a;
template AA<char> B<char>::aa;
template AA<char> B<char>::aa2;

-- 


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


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

* [Bug c++/19610] default constructor not called for static template member of template class
  2005-01-24 20:22 [Bug c++/19610] New: default constructor not called for static template member of template class jamesp at trdlnk dot com
  2005-01-24 20:38 ` [Bug c++/19610] " pinskia at gcc dot gnu dot org
@ 2005-01-24 21:19 ` jamesp at trdlnk dot com
  2005-01-26  4:16 ` bangerth at dealii dot org
  2 siblings, 0 replies; 4+ messages in thread
From: jamesp at trdlnk dot com @ 2005-01-24 21:19 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From jamesp at trdlnk dot com  2005-01-24 21:19 -------
I've tried what you suggested, and it did work, but now I'm confused. Why was
the constructor for aa2 called in the original example? Based on what you have
said, it sounds like that should not have happened until aa2 was instantiated.
If it is working properly then I don't understand why the extra line to
instantiate aa2 is necessary in your fix. The fact that I specialized them all
the same way and some of them were constructed and others weren't seems odd to me.

-- 


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


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

* [Bug c++/19610] default constructor not called for static template member of template class
  2005-01-24 20:22 [Bug c++/19610] New: default constructor not called for static template member of template class jamesp at trdlnk dot com
  2005-01-24 20:38 ` [Bug c++/19610] " pinskia at gcc dot gnu dot org
  2005-01-24 21:19 ` jamesp at trdlnk dot com
@ 2005-01-26  4:16 ` bangerth at dealii dot org
  2 siblings, 0 replies; 4+ messages in thread
From: bangerth at dealii dot org @ 2005-01-26  4:16 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2005-01-26 04:16 -------
The declaration of a specialization is not a definition, unless it has 
an explicit initializer call. The way you want to write this is  
as follows: 
  template <> A<char> B<char>::a = A<char>(); 
The standard specifically says that this is the only permissible 
syntax for default initialization. 
 
W. 
 
 

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


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


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

end of thread, other threads:[~2005-01-26  4:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-24 20:22 [Bug c++/19610] New: default constructor not called for static template member of template class jamesp at trdlnk dot com
2005-01-24 20:38 ` [Bug c++/19610] " pinskia at gcc dot gnu dot org
2005-01-24 21:19 ` jamesp at trdlnk dot com
2005-01-26  4:16 ` 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).