Topic: static template data member


Author: Gabriel <xiaos@lucent.com>
Date: Mon, 12 Feb 2001 14:44:37 GMT
Raw View
It is correct, because compiler will see this as a special case and merge
all duplicated defination into one defination ( according to one defination
rule in c++). Meantime, you MUST put this static definition in your
header files.

Gil Shafriri wrote:

  I have simple class that includes static data  member defined  in some
  header:

  class X

  {

  public:

  static char* m_c;

  };

  char* X::m_c;// static member definition

  When this  header is included from multiple source files - my linker
  produces  an error message claming for multiple definition

  of the member X::m_c. This seemed to be logical.

  On the other hand, I have in another header file a template class

  template <class T> class TX

  {

  public:

  static char* m_c;

  };

  template <class T> char* X<T>::m_c;

  This template can be included from multiple source files without any
  problem.

  - Is it the correct compiler behavior ? Could you help me in finding the
  specific pointer in the standard regarding the correctness of this code ?


  Thanks,

  Gil

  ---
  [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
  [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
  [              --- Please see the FAQ before posting. ---               ]
  [ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
  [ Note that the FAQ URL has changed!  Please update your bookmarks.     ]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Gil Shafriri" <gilsh@microsoft.com>
Date: Thu, 8 Feb 2001 07:04:17 GMT
Raw View
I have simple class that includes static data  member defined  in some
header:

class X

{

public:

static char* m_c;

};

char* X::m_c;// static member definition

When this  header is included from multiple source files     my linker
produces  an error message claming for multiple definition

of the member X::m_c. This seemed to be logical.

On the other hand, I have in another header file a template class

template <class T> class TX

{

public:

static char* m_c;

};

template <class T> char* X<T>::m_c;

This template can be included from multiple source files without any
problem.

- Is it the correct compiler behavior ? Could you help me in finding the
specific pointer in the standard regarding the correctness of this code ?

Thanks,

Gil







---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Thu, 8 Feb 2001 18:08:48 GMT
Raw View
In article <3a81c0ca@news.microsoft.com>, Gil Shafriri
<gilsh@microsoft.com> writes
>I have simple class that includes static data  member defined  in some
>header:
>
>class X
>
>{
>
>public:
>
>static char* m_c;
>
>};
>
>char* X::m_c;// static member definition
>
>When this  header is included from multiple source files =E2=80=93 my li=
nker
>produces  an error message claming for multiple definition
Correct

>
>of the member X::m_c. This seemed to be logical.
>
>On the other hand, I have in another header file a template class
>
>template <class T> class TX
>
>{
>
>public:
>
>static char* m_c;
>
>};
>
>template <class T> char* X<T>::m_c;
>
>This template can be included from multiple source files without any
>problem.
>
>- Is it the correct compiler behavior ? Could you help me in finding the
>specific pointer in the standard regarding the correctness of this code =
?

Also correct because some device has to be used to deal with multiple
instantiations of a template with the same template parameters. We
cannot make the code an error and use the inclusion model for templates.


--=20
Francis Glassborow
See http://www.accu.org for details of The ACCU Spring Conference, 2001
(includes many regular participants to C & C++ newsgroups)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: Fri, 9 Feb 2001 03:31:51 GMT
Raw View
In article <3a81c0ca@news.microsoft.com>,
  "Gil Shafriri" <gilsh@microsoft.com> wrote:
My goodness, template static data members is a hot topic this week!

> On the other hand, I have in another header file a template class
>
> template <class T> class TX
>
> {
>
> public:
>
> static char* m_c;
>
> };
>
> template <class T> char* X<T>::m_c;
>
> This template can be included from multiple source files without any
> problem.
>
> - Is it the correct compiler behavior ? Could you help me in finding
> the
> specific pointer in the standard regarding the correctness of this
> code ?
Yes, this is correct. The "One Definition Rule" (clause 3.2) says you
can define a class template's static data member in multiple translation
units, subject to some restrictions.

--
Jim
To suppress Deja's product links, add this header:
x-no-productlinks: yes


Sent via Deja.com
http://www.deja.com/

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]