public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/15935] New: Defining static [const] member variables of template class
@ 2004-06-11  8:49 Rainer dot Bensch at rsd dot rohde-schwarz dot com
  2004-06-11 13:28 ` [Bug c++/15935] " pinskia at gcc dot gnu dot org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Rainer dot Bensch at rsd dot rohde-schwarz dot com @ 2004-06-11  8:49 UTC (permalink / raw)
  To: gcc-bugs

static [const] member variables of template classes, std::basic_string for 
instance, are not always emitted by the compiler.

//--- with gcc 3.2 PowerPC
//--- as tge.cpp:

#undef  _GLIBCPP_NO_TEMPLATE_EXPORT
#define _GLIBCPP_NO_TEMPLATE_EXPORT 1

#include <string>
#include <iostream>

namespace other
{

struct my_char_traits : public std::char_traits<char> {};

typedef std::basic_string<char, my_char_traits> my_string;

}


using namespace std;
using namespace other;

int main (int, const char*[])
{
   my_string x ("it works!");
   cout << x.c_str() << endl;

   return 0;
}

//--------------------------------------------------------- 
//---compiled with:
// -c -frtti -fno-gnu-keywords -msoft-float -fmerge-all-constants -O2
// -fno-inline-functions -fno-implement-inlines -Winline -Wall
// -Woverloaded-virtual -Wsynth -Wreturn-type -Wno-ctor-dtor-privacy -Wmultichar
// -Wfloat-equal -fno-weak -Wno-parentheses -g
//---------------------------------------------------------
//---linked with:
// --warn-once -nostdlib -M --cref -Map ...map -o ...elf -T ...lcf crt0.o 
// -(TEST_2.a libgcc.a libm.a libstdc++.a libsupc++.a libc.a ... -)

//---link errors:
// TEST_2.a(tge.o): In function `main':
// /cygdrive/include/c++/3.2/bits/char_traits.h:119: 
// undefined reference to `std::basic_string<char, other::my_char_traits, 
std::allocator<char> >::_Rep::_S_terminal'

// TEST_2.a(tge.o): In function `std::basic_string<char, other::my_char_traits, 
std::allocator<char> >::_Rep::_S_create(unsigned, std::allocator<char> const&)':
// /cygdrive/include/c++/3.2/bits/basic_string.tcc:388: 
// undefined reference to `std::basic_string<char, other::my_char_traits, 
std::allocator<char> >::_Rep::_S_max_size'

//--------------------------------------------------------- 

// 1) The templates defining both variables was seen by the compiler before 
//    instantiation of class my_string.
// 2) The templates work for std::string (if not otherwise resolved from a 
library)
// 3) Such templates defining static const variables declared in template 
classes
//    usually work fine. Must have something to do with mixing namespaces in
//    template parameters or declaring the class instance in another namespace,
//    I guess.
// 4) Workaround (neither portable nor resistant against stl changes):

// enable workaround
#if 0

// either one of the following works
#if 0
    namespace other
#else
    namespace std
#endif
    {

    const char   basic_string<char, other::my_char_traits>::_Rep::_S_terminal = 
char();
    const size_t basic_string<char, other::my_char_traits>::_Rep::_S_max_size = 
       (((basic_string<char, other::my_char_traits>::npos 
       - sizeof(basic_string<char, other::my_char_traits>::_Rep))/sizeof
(char)) - 1) / 4;

    }
#endif

-- 
           Summary: Defining static [const] member variables of template
                    class
           Product: gcc
           Version: 3.2
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Rainer dot Bensch at rsd dot rohde-schwarz dot com
                CC: gcc-bugs at gcc dot gnu dot org


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


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

* [Bug c++/15935] Defining static [const] member variables of template class
  2004-06-11  8:49 [Bug c++/15935] New: Defining static [const] member variables of template class Rainer dot Bensch at rsd dot rohde-schwarz dot com
@ 2004-06-11 13:28 ` pinskia at gcc dot gnu dot org
  2004-06-17  8:23 ` Rainer dot Bensch at rsd dot rohde-schwarz dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-06-11 13:28 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-06-11 13:28 -------
Not a bug what you do as a workaround is the correct way of doing it if you do not have weak symbols.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID
   Target Milestone|---                         |3.4.0


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


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

* [Bug c++/15935] Defining static [const] member variables of template class
  2004-06-11  8:49 [Bug c++/15935] New: Defining static [const] member variables of template class Rainer dot Bensch at rsd dot rohde-schwarz dot com
  2004-06-11 13:28 ` [Bug c++/15935] " pinskia at gcc dot gnu dot org
@ 2004-06-17  8:23 ` Rainer dot Bensch at rsd dot rohde-schwarz dot com
  2004-06-17  8:47 ` pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Rainer dot Bensch at rsd dot rohde-schwarz dot com @ 2004-06-17  8:23 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From Rainer dot Bensch at rsd dot rohde-schwarz dot com  2004-06-17 08:23 -------
>> what you do as a workaround is the correct way of doing it ...

I thought about it but can't agree.

- what is the sense of having a template for defining the variable when an 
explicit specialization is always needed?

- how do I get all the hundreds of variables and their names from classes deep 
in a complex class hierachy I inherited from foreign sources, e.g. from GNU, 
explicitly defined when my_class is also a template? You won't like to impose 
such a permanent and silly job on me or my customers, don't you?

>> ... if you do not have weak symbols
I also don't see how weak symbols may solve the problem.

Cheers, R.B.


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


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


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

* [Bug c++/15935] Defining static [const] member variables of template class
  2004-06-11  8:49 [Bug c++/15935] New: Defining static [const] member variables of template class Rainer dot Bensch at rsd dot rohde-schwarz dot com
  2004-06-11 13:28 ` [Bug c++/15935] " pinskia at gcc dot gnu dot org
  2004-06-17  8:23 ` Rainer dot Bensch at rsd dot rohde-schwarz dot com
@ 2004-06-17  8:47 ` pinskia at gcc dot gnu dot org
  2004-06-17  8:49 ` pinskia at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-06-17  8:47 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-06-17 08:47 -------
Nope (the reason why the one with the namespace in other works is a different bug) this is still invalid 
as the standard requires what you are doing for a work around, sorry but that is C++.

-- 


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


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

* [Bug c++/15935] Defining static [const] member variables of template class
  2004-06-11  8:49 [Bug c++/15935] New: Defining static [const] member variables of template class Rainer dot Bensch at rsd dot rohde-schwarz dot com
                   ` (2 preceding siblings ...)
  2004-06-17  8:47 ` pinskia at gcc dot gnu dot org
@ 2004-06-17  8:49 ` pinskia at gcc dot gnu dot org
  2004-06-17 13:25 ` bangerth at dealii dot org
  2004-06-18 11:35 ` Rainer dot Bensch at rsd dot rohde-schwarz dot com
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-06-17  8:49 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-06-17 08:49 -------
Forgot to close.

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


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


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

* [Bug c++/15935] Defining static [const] member variables of template class
  2004-06-11  8:49 [Bug c++/15935] New: Defining static [const] member variables of template class Rainer dot Bensch at rsd dot rohde-schwarz dot com
                   ` (3 preceding siblings ...)
  2004-06-17  8:49 ` pinskia at gcc dot gnu dot org
@ 2004-06-17 13:25 ` bangerth at dealii dot org
  2004-06-18 11:35 ` Rainer dot Bensch at rsd dot rohde-schwarz dot com
  5 siblings, 0 replies; 7+ messages in thread
From: bangerth at dealii dot org @ 2004-06-17 13:25 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-06-17 13:24 -------
The reason weak symbols appear here is that if your OS _does_ support weak 
symbols, the definition of the template member can appear in a header 
file. However, if you don't have weak symbol support, then this would 
lead to multiple symbol definition errors, so one has to instantiate 
these variables one by one in some implementation file. 
 
Does the situation improve with newer versions of gcc? And what OS are you 
actually on? 
 
W. 

-- 


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


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

* [Bug c++/15935] Defining static [const] member variables of template class
  2004-06-11  8:49 [Bug c++/15935] New: Defining static [const] member variables of template class Rainer dot Bensch at rsd dot rohde-schwarz dot com
                   ` (4 preceding siblings ...)
  2004-06-17 13:25 ` bangerth at dealii dot org
@ 2004-06-18 11:35 ` Rainer dot Bensch at rsd dot rohde-schwarz dot com
  5 siblings, 0 replies; 7+ messages in thread
From: Rainer dot Bensch at rsd dot rohde-schwarz dot com @ 2004-06-18 11:35 UTC (permalink / raw)
  To: gcc-bugs


------- 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


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

end of thread, other threads:[~2004-06-18 11:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-11  8:49 [Bug c++/15935] New: Defining static [const] member variables of template class Rainer dot Bensch at rsd dot rohde-schwarz dot com
2004-06-11 13:28 ` [Bug c++/15935] " pinskia at gcc dot gnu dot org
2004-06-17  8:23 ` Rainer dot Bensch at rsd dot rohde-schwarz dot com
2004-06-17  8:47 ` pinskia at gcc dot gnu dot org
2004-06-17  8:49 ` pinskia at gcc dot gnu dot org
2004-06-17 13:25 ` bangerth at dealii dot org
2004-06-18 11:35 ` Rainer dot Bensch at rsd dot rohde-schwarz dot com

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).