This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/15935] Defining static [const] member variables of template class
- From: "Rainer dot Bensch at rsd dot rohde-schwarz dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 18 Jun 2004 11:35:26 -0000
- Subject: [Bug c++/15935] Defining static [const] member variables of template class
- References: <20040611084936.15935.Rainer.Bensch@rsd.rohde-schwarz.com>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Additional Comments From Rainer dot Bensch at rsd dot rohde-schwarz dot com 2004-06-18 11:35 -------
Thank you for all the help but the correct handling of the problem is an
explicit instantiation of the template like
template class std::basic_string<char, my_char_traits>;
BTW,
there must be a serious misunderstanding of what C++ is and what it isn't among
the people in the FSF. Let's have a look at a GCC system header:
from basic_string.tcc:
---------------------
// Components for manipulating sequences of characters -*- C++ -*-
// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
// Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
------------------------------- snip
... continued:
//
// ISO C++ 14882: 21 Strings library
//
// This file is included by <string>. It is not meant to be included
// separately.
// Written by Jason Merrill based upon the specification by Takanori Adachi
// in ANSI X3J16/94-0013R2. Rewritten by Nathan Myers to ISO-14882.
#ifndef _CPP_BITS_STRING_TCC
#define _CPP_BITS_STRING_TCC 1
#pragma GCC system_header
namespace std
{
template<typename _CharT, typename _Traits, typename _Alloc>
const typename basic_string<_CharT, _Traits, _Alloc>::size_type
basic_string<_CharT, _Traits, _Alloc>::
_Rep::_S_max_size = (((npos - sizeof(_Rep))/sizeof(_CharT)) - 1) / 4;
template<typename _CharT, typename _Traits, typename _Alloc>
const _CharT
basic_string<_CharT, _Traits, _Alloc>::
_Rep::_S_terminal = _CharT();
------------------------------- snip
So, what was I told? The code shown isn't useful in C++. Because defining such
templates simply makes no sense. GCC looks at it, it conforms to the syntax
rules but it never will have any effect on the compiler output. It is supposed
to be ignored because "C++ requires that." (says one part of the FSF)
Why then was it written at all (by another part of the FSF)?
(I'll stop here because I don't like the sarcasm to get any heavier...)
Now, the C++ 14882 mentioned above says in
14.7.1
(7) The implicit instantiation of the class template does not cause any static
data members of that class to be implicitly instantiated.
[ that does not contradict or supersede 14.7.1 (1) because of ]
...unless the static member is itself used in a way that requires the
definition ... to exist.
[ what it does. ]
Cheers, R.B.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15935