delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2006/01/21/14:46:08

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: "Alon Neubach" <alonneubach AT gmail DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: template specialization problem
Lines: 57
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-NNTP-Posting-Host: l85-130-135-131.broadband.actcom.net.il
Message-ID: <newscache$3djgti$vp7$1@news.actcom.co.il>
Date: Sat, 21 Jan 2006 21:24:03 +0200
NNTP-Posting-Host: 192.114.47.10
X-Complaints-To: abuse AT verio DOT net
X-Trace: newsread1.mlpsca01.us.to.verio.net 1137871625 192.114.47.10 (Sat, 21 Jan 2006 19:27:05 GMT)
NNTP-Posting-Date: Sat, 21 Jan 2006 19:27:05 GMT
Organization: NTT/VERIO
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hi
I'm using DJGPP C++ compiler version 4.01.
I tried compiling this program, that uses a static data member in a 
template: I tried to declare the data member in a specialization of the 
template B (to an int for example). The data member is of type A, which is a 
class.
The funy thing is, when I declare the member outside the class with A's 
default constructor, I get a linker error: "undefined reference to 
`B<int>::_a'", but when I use A's constructor that receives a parameter, the 
problem disappears. Also, if I assign a new A object to the member, using "a 
= A()", the problem also disappears - see in code below.
Note that the same thing happens if the member is of internal type - then it 
can be solved only by assigning it with an initial value.

I did manage to get the original program to compile and link in Visual 
Studio 6, so I was wondering if this was a bug in the compiler, or is this 
supposed to be the behavior of the language?

This is the (simplified) program I tried compiling - all in one cpp file:

class A
{
public:
    A() {}
    A(int dummy) {}
    void foo() {}
};

template <class T>
class B
{
public:
    static A _a;
};

template <> A B<int>::_a; //This line causes a linker error

int main(int argc, char* argv[])
{
    B<int>::_a.foo();
    return 0;
}

(end of program)

When I chage the problematic line to this:
template <> A B<int>::_a(0); //This uses A's second ctor

or this:
template <> A B<int>::_a = A(); //Use A's default ctor explicitly

The problem disappears...

Thanks,
    Alon 


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019