| View previous topic :: View next topic |
| Author |
Message |
Michal Khorne Rzechonek Guest
|
Posted: Wed Jan 19, 2005 12:00 am Post subject: static attributes of template classes |
|
|
Hello there
I got a problem with linking programs like
template<typename T>
class foo {
static int bar;
};
In-class initialization is forbidden by the language itself, and I have no
iterest in explicit instantiation.
How should I write the code to link it without any undefined refrences?
gcc 3.3.1
best regards
--
Michal "Khorne" Rzechonek -- mail&jabber khorne(at)leto.homedns.org
Moloch: http://leto.homedns.org/~moloch
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Laurence Finston Guest
|
Posted: Wed Jan 19, 2005 9:22 pm Post subject: Re: static attributes of template classes |
|
|
Hello Michal,
On Wed, 18 Jan 2005, Michal Khorne Rzechonek wrote:
| Quote: |
I got a problem with linking programs like
template
class foo {
static int bar;
};
In-class initialization is forbidden by the language itself, and I have no
iterest in explicit instantiation.
|
You might have to do it anyway.
| Quote: | How should I write the code to link it without any undefined refrences?
gcc 3.3.1
|
See section 6.6 "Where's the Template?" in the GCC manual. I had to
read it a couple of times before I understood it.
http://gcc.gnu.org/onlinedocs/gcc-3.3.5/gcc/
Template-Instantiation.html#Template-Instantiation
If you want to see how I've handled the problem, my code is
available at
http://savannah.gnu.org/cgi-bin/viewcvs/3dldf/3dldf/Group/CWEB/
Laurence Finston
http://www.gnu.org/software/3dldf/LDF.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Markus Moll Guest
|
Posted: Wed Jan 19, 2005 11:47 pm Post subject: Re: static attributes of template classes |
|
|
Hi
Michal Khorne Rzechonek wrote:
| Quote: | template<typename T
class foo {
static int bar;
};
How should I write the code to link it without any undefined refrences?
|
Quote (14.5.1.3 static data members of class templates):
"A definition for a static data member may be provided in a namespace scope
enclosing the definition of the static member s class template."
In your case:
template
class foo {
static int bar;
};
template<typename T> int foo<T>::bar = 42;
Markus
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Wed Jan 19, 2005 11:48 pm Post subject: Re: static attributes of template classes |
|
|
On 18 Jan 2005 19:00:32 -0500, Michal Khorne Rzechonek
<khorne (AT) leto (DOT) homedns.org> wrote:
| Quote: | I got a problem with linking programs like
template<typename T
class foo {
static int bar;
};
In-class initialization is forbidden by the language itself, and I have
no iterest in explicit instantiation.
|
It's allowed for static const integral members. (for example: static int
const bar = 0xaa).
| Quote: | How should I write the code to link it without any undefined refrences?
|
If you do need a non const static integral member just add in the header:
template
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Wed Jan 19, 2005 11:50 pm Post subject: Re: static attributes of template classes |
|
|
Hi,
Michal Khorne Rzechonek wrote:
| Quote: | I got a problem with linking programs like
template<typename T
class foo {
static int bar;
};
In-class initialization is forbidden by the language itself, and I have no
iterest in explicit instantiation.
|
Add something like this outside the class definition:
template
int foo<T>::bar = 7;
gcc 3.4.2
Pozdrawiam,
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Wed Jan 19, 2005 11:51 pm Post subject: Re: static attributes of template classes |
|
|
Michal Khorne Rzechonek wrote:
| Quote: | Hello there
I got a problem with linking programs like
template<typename T
class foo {
static int bar;
};
In-class initialization is forbidden by the language itself, and I have no
iterest in explicit instantiation.
How should I write the code to link it without any undefined refrences?
|
You declared foo
be given at namespace scope, just like data members of a regular
(non-templated) class. Just put:
template<typename T>
int foo<T>::bar;
somewhere after the declaration of foo, possibly in the same header file
(if foo is declared in a header). Notice that the definition could also
include an initialization, if needed:
template<typename T>
int foo<T>::bar = 1;
HTH,
Alberto
PS: this one should be added to the FAQs, I guess...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
luke Guest
|
Posted: Wed Jan 19, 2005 11:55 pm Post subject: Re: static attributes of template classes |
|
|
Michal Khorne Rzechonek wrote:
| Quote: | Hello there
I got a problem with linking programs like
template
class foo {
static int bar;
};
In-class initialization is forbidden by the language itself, and I
have no
iterest in explicit instantiation.
How should I write the code to link it without any undefined
refrences?
gcc 3.3.1
|
You can put the static attribute to a base class of this template class.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Steven E. Harris Guest
|
Posted: Thu Jan 20, 2005 12:23 pm Post subject: Re: static attributes of template classes |
|
|
"Maxim Yegorushkin" <e-maxim (AT) yandex (DOT) ru> writes:
| Quote: | If you do need a non const static integral member just add in the
header:
template<class T> int foo<T>::bar;
|
And the follow-up question usually asks, "But how many separate ints
called foo<T>::bar will wind up in the final compiled program?"
--
Steven E. Harris
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
dietmar_kuehl@yahoo.com Guest
|
Posted: Thu Jan 20, 2005 12:24 pm Post subject: Re: static attributes of template classes |
|
|
Michal Khorne Rzechonek wrote:
| Quote: | I got a problem with linking programs like
template<typename T
class foo {
static int bar;
};
How should I write the code to link it without any undefined
refrences? |
You should add a definition:
If this gives you multiple declarations when using the instantiation
in multiple translation units, you could wrap things into a static
function instead:
| Quote: | template <typename T
class foo {
static int& bar() { static int rc = 0; return rc; }
};
|
The value returned by 'bar()' will be retained as it returns a
reference to a static variable.
--
<http://www.contendix.com> - Software Development & Consulting
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Thu Jan 20, 2005 6:24 pm Post subject: Re: static attributes of template classes |
|
|
On 20 Jan 2005 07:23:28 -0500, Steven E. Harris <seh (AT) panix (DOT) com> wrote:
| Quote: | "Maxim Yegorushkin" <e-maxim (AT) yandex (DOT) ru> writes:
If you do need a non const static integral member just add in the
header:
template<class T> int foo<T>::bar;
And the follow-up question usually asks, "But how many separate ints
called foo<T>::bar will wind up in the final compiled program?"
|
Then could you quote a typical answer please?
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Thu Jan 20, 2005 6:30 pm Post subject: Re: static attributes of template classes |
|
|
On 20 Jan 2005 07:24:43 -0500, [email]dietmar_kuehl (AT) yahoo (DOT) com[/email]
<dietmar_kuehl (AT) yahoo (DOT) com> wrote:
[]
| Quote: | You should add a definition:
| template <typename T> int foo<T>::int = 0;
If this gives you multiple declarations when using the instantiation
in multiple translation units, you could wrap things into a static
function instead:
|
[]
Could you please elaborate?
My current undestanding is that you won't get multiple declaration error
because it does not violate ODR.
There was a thread with a trick for placing statics in a header instead of
cpp: http://groups-beta.google.com/group/comp.lang.c++/msg/daccfae940faed74
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Thu Jan 20, 2005 8:04 pm Post subject: Re: static attributes of template classes |
|
|
Maxim Yegorushkin wrote:
| Quote: | template<class T> int foo<T>::bar;
And the follow-up question usually asks, "But how many separate ints
called foo<T>::bar will wind up in the final compiled program?"
Then could you quote a typical answer please?
|
"One for each <T>."
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dietmar Kuehl Guest
|
Posted: Fri Jan 21, 2005 10:33 am Post subject: Re: static attributes of template classes |
|
|
Maxim Yegorushkin wrote:
| Quote: | My current undestanding is that you won't get multiple declaration error
because it does not violate ODR.
|
So is mine. Last time I tried, I found compilers which disagreed.
Since we are programming for the real world, we need to use real
world compiers and for some of those the static function approach
worked while the static member approach did not.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Steven E. Harris Guest
|
Posted: Sat Jan 22, 2005 5:12 am Post subject: Re: static attributes of template classes |
|
|
"Maxim Yegorushkin" <e-maxim (AT) yandex (DOT) ru> writes:
| Quote: | Then could you quote a typical answer please?
|
It depends on your compiler's template instantiation model and whether
the linker can remove would-be duplicates from object files.
--
Steven E. Harris
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Sat Jan 22, 2005 6:43 pm Post subject: Re: static attributes of template classes |
|
|
Steven E. Harris wrote:
| Quote: | Then could you quote a typical answer please?
It depends on your compiler's template instantiation model and whether
the linker can remove would-be duplicates from object files.
|
The standard is quite clear on this subject. The relevant section is ยง3.2.5
<quote>
There can be more than one definition of a class type (clause 9),
enumeration type (7.2), inline function with external linkage (7.1.2),
class template (clause 14), nonstatic
function template (14.5.5), *static data member of a class template
(14.5.1.3)*, member function template (14.5.1.1), or template
specialization for which some template parameters are not specified (14.7,
14.5.4) in a program provided that each definition appears in a different
translation unit, and provided the definitions satisfy the following
requirements.
....
</quote>
If your compiler does not satisfy the clause, then it's nonstandard - all
best are off.
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|