Topic: Can this problem be solved?


Author: newshutz@charlie.rchland.ibm.com (Robert Newshutz)
Date: 1995/06/14
Raw View
In article <DA2vLv.Aru@firewall.dcc.com>, mross@DCC.COM (Michael Ross) writes:
|> In article <D9wHon.Kp@ddvictor.dundee.ATTGIS.COM>, <Unknown> says...
|> >
|> >
|> >I no C++ expert and I not sure if what I am about to say is correct, but
|> I
|> >think that the problem is not actually a bug. When a variable is defined
|> as
|> >static in a class there is only one instance of that variable, which I
|> suppose
|> >is effectively giving it global scope without the ability to reference
|> it
|> >globally. This is not normally a problem, but when used with templates
|> where
|> >each compilation unit gets a new definition of the class a new static is
|> >created for that compilation unit. When it comes to link time there are
|> >mutiple instances of the static with different addresses. I don't know
|> of any
|> >way to get round this problem other than making the class static a real
|> ( not
|> >float :) ) global (which only needs be defined in one file with an
|> extern in
|> >your template header file) and creating a reference to it in the class,
|> this
|> >should give you the desired effect but its not very elegant. I hope that
|> this
|> >is useful, even better if its also correct.
|> >
|> >                                Jim
|> >
|> >
|> I read the responses to this question, and it seems everyone
|> mis-understood your question. The problem is that you're using
|> _templates_ with these statics. Templates are essentially just a sort of
|> pre-compilation trick, like a pre-processor #define. When you actually go
|> to compile the template, say:
|>
|> template <class T> class Vector { ... };
|>
|> class Vector<char *> { ... };
|> class Vector<int> { ... };
|>
|> the two Vector classes actually get compiled as if you had written the
|> whole definition out twice:
|>
|> class Vectorchar { ... };
|> class Vectorint { ... };
|>
|> they are completely different, so the static members are completely
|> different. All the template stuff allows you to do is to use some
|> shorthand in defining the classes, but they compile as if you had written
|> them out the long way.
|>
|> I think to do what you want, you have to have a real static global
|> variable, as you said, and have a reference to it...
|>
|> -Michael Ross
|>
|>
|>
|>
Or derive the templates from a single base class with the static data.

Robert N. Newshutz   newshutz@vnet.ibm.com, newshutz@aol.com





Author: "G nter Nagler" <gnagler@ihm.tu-graz.ac.at>
Date: 1995/06/12
Raw View
jason@cygnus.com (Jason Merrill) wrote:
>>>>>> Pierre Baillargeon <dak@cae.ca> writes:
>
>> The trick to avoid the multiple definition of static member defined in
a header
>> is to make the definition it self static:
>
>> --- class.h ---
>
>> class A
>> {
>>  static int a;
>> };
>
>> static int A::a;
>
>> --- EOF ---
>
>> This is legal since it is used in the iostream class to force
>> initialization of the stream before ANY other class simply by
including
>> the stream header. (See june issue of C/C++ journal).
>
>No, it isn't legal.
>
>  9.5.2  Static data members                         [class.static.data]
>
>5 There shall be exactly one definition of a static  data  member  in  a
>  program; no diagnostic is required; see _basic.def.odr_.

I see only one definition of A::a

>
>6 Static  data members of a class in namespace scope have external link-
>  age (_basic.link_).  A local class shall not have static data members.
>

I do not see here any statement that says that static members of the
above form are not legal. (6) say only that static members are not
legal in "local" classes (nothing about global classes as used above).

Guenter
-----------------------------------------------------------------
Guenter Nagler
JOANNEUM RESEARCH, Institute for HyperMedia Systems (IHM)
Schieszstattgasse 4A, A-8010 Graz, Austria
email: gnagler@ihm.tu-graz.ac.at







Author: mross@DCC.COM (Michael Ross)
Date: 1995/06/12
Raw View
In article <D9wHon.Kp@ddvictor.dundee.ATTGIS.COM>, <Unknown> says...
>
>
>I no C++ expert and I not sure if what I am about to say is correct, but
I
>think that the problem is not actually a bug. When a variable is defined
as
>static in a class there is only one instance of that variable, which I
suppose
>is effectively giving it global scope without the ability to reference
it
>globally. This is not normally a problem, but when used with templates
where
>each compilation unit gets a new definition of the class a new static is
>created for that compilation unit. When it comes to link time there are
>mutiple instances of the static with different addresses. I don't know
of any
>way to get round this problem other than making the class static a real
( not
>float :) ) global (which only needs be defined in one file with an
extern in
>your template header file) and creating a reference to it in the class,
this
>should give you the desired effect but its not very elegant. I hope that
this
>is useful, even better if its also correct.
>
>                                Jim
>
>
I read the responses to this question, and it seems everyone
mis-understood your question. The problem is that you're using
_templates_ with these statics. Templates are essentially just a sort of
pre-compilation trick, like a pre-processor #define. When you actually go
to compile the template, say:

template <class T> class Vector { ... };

class Vector<char *> { ... };
class Vector<int> { ... };

the two Vector classes actually get compiled as if you had written the
whole definition out twice:

class Vectorchar { ... };
class Vectorint { ... };

they are completely different, so the static members are completely
different. All the template stuff allows you to do is to use some
shorthand in defining the classes, but they compile as if you had written
them out the long way.

I think to do what you want, you have to have a real static global
variable, as you said, and have a reference to it...

-Michael Ross









Author: jason@cygnus.com (Jason Merrill)
Date: 1995/06/12
Raw View
>>>>> Gunter Nagler <gnagler@ihm.tu-graz.ac.at> writes:

> jason@cygnus.com (Jason Merrill) wrote:
>>>>>>> Pierre Baillargeon <dak@cae.ca> writes:
>>
>>> The trick to avoid the multiple definition of static member defined in
> a header
>>> is to make the definition it self static:
>>
>>> --- class.h ---
>>
>>> class A
>>> {
>>> static int a;
>>> };
>>
>>> static int A::a;
>>
>>> --- EOF ---

>> 5 There shall be exactly one definition of a static  data  member  in  a
>> program; no diagnostic is required; see _basic.def.odr_.

> I see only one definition of A::a

If you include class.h in two files, you get two definitions of A::a.

>> 6 Static  data members of a class in namespace scope have external link-
>> age (_basic.link_).  A local class shall not have static data members.

> I do not see here any statement that says that static members of the
> above form are not legal. (6) say only that static members are not
> legal in "local" classes (nothing about global classes as used above).

It also says that static data members of a class in namespace scope [which
includes file scope] have external [i.e. not static] linkage.

Jason





Author: jimw <>
Date: 1995/06/09
Raw View
I no C++ expert and I not sure if what I am about to say is correct, but I
think that the problem is not actually a bug. When a variable is defined as
static in a class there is only one instance of that variable, which I suppose
is effectively giving it global scope without the ability to reference it
globally. This is not normally a problem, but when used with templates where
each compilation unit gets a new definition of the class a new static is
created for that compilation unit. When it comes to link time there are
mutiple instances of the static with different addresses. I don't know of any
way to get round this problem other than making the class static a real ( not
float :) ) global (which only needs be defined in one file with an extern in
your template header file) and creating a reference to it in the class, this
should give you the desired effect but its not very elegant. I hope that this
is useful, even better if its also correct.

    Jim







Author: jimw <>
Date: 1995/06/09
Raw View
==========jimw, 09/06/95==========

>I no C++ expert and I not sure if what I am about to say is correct, but I
>think that the problem is not actually a bug. When a variable is defined as
>static in a class there is only one instance of that variable, which I
suppose
>is effectively giving it global scope without the ability to reference it
>globally. This is not normally a problem, but when used with templates where
>each compilation unit gets a new definition of the class a new static is
>created for that compilation unit. When it comes to link time there are
>mutiple instances of the static with different addresses. I don't know of
any
>way to get round this problem other than making the class static a real (
not
>float :) ) global (which only needs be defined in one file with an extern in
>your template header file) and creating a reference to it in the class, this
>should give you the desired effect but its not very elegant. I hope that
this
>is useful, even better if its also correct.
>
>    Jim


I found the following reference in the Microsoft C++ Language Reference P.245
sec 8.5 and I quote :

"Static data members are not part of objects of a given class type; they are
separate objects. As a result, the declaration of a static data member is not
considered a definition. The data member is declared in class scope, but
definition is performed at file scope. These static members have external
linkage."

    Jim








Author: dak@cae.ca (Pierre Baillargeon)
Date: 1995/06/09
Raw View
The trick to avoid the multiple definition of static member defined in a header
is to make the definition it self static:

--- class.h ---

class A
{
 static int a;
};

static int A::a;

--- EOF ---

This is legal since it is used in the iostream class to force initialization of
the stream before ANY other class simply by including the stream header. (See
june issue of C/C++ journal).






Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/06/09
Raw View
In article 15260@cae.ca, dak@cae.ca (Pierre Baillargeon) writes:

>The trick to avoid the multiple definition of static member defined in a header
>is to make the definition it self static:
>
>class A
>{
> static int a;
>};
>
>static int A::a;

Maybe your compiler accepts this code, but it isn't valid C++. From the
draft standard, section 9.5.2, "Static Data Members":

"Static data members of a class in namespace scope have external linkage
(3.5). A local class shall not have static data members."

---
Steve Clamage, stephen.clamage@eng.sun.com







Author: jason@cygnus.com (Jason Merrill)
Date: 1995/06/09
Raw View
>>>>> Pierre Baillargeon <dak@cae.ca> writes:

> The trick to avoid the multiple definition of static member defined in a header
> is to make the definition it self static:

> --- class.h ---

> class A
> {
>  static int a;
> };

> static int A::a;

> --- EOF ---

> This is legal since it is used in the iostream class to force
> initialization of the stream before ANY other class simply by including
> the stream header. (See june issue of C/C++ journal).

No, it isn't legal.

  9.5.2  Static data members                         [class.static.data]

5 There shall be exactly one definition of a static  data  member  in  a
  program; no diagnostic is required; see _basic.def.odr_.

6 Static  data members of a class in namespace scope have external link-
  age (_basic.link_).  A local class shall not have static data members.






Author: sgr@ae-sun01.alden.com (Stan Ryckman)
Date: 1995/06/09
Raw View
Hi folks,

Would you please confine c++ discussions to the c++ groups?
This has nothing to do with the C Standard.

Note followups.  Thank you very much.

Cheers,
Stan.
--
HELP!  I'M BEING HELD PRISONER IN A .SIGNATURE FACTORY!
    Stan Ryckman   sgr@alden.com





Author: mike@if.com (MIke Homyack - System Owner)
Date: 1995/06/08
Raw View
On 7 Jun 1995 05:06:51 GMT, Kalyan Kolachala (kal@chromatic.com) wrote:
 - Thanks for all the responses I got. But it seems that in
 - attempting to present the problem in simple and general
 - terms I've made it look like something else. Actually
 - the problem caused by a bug in Visual C++ 2.0/2.1 made
 - me wish there was a feature (TBD later) that could be highly
 - useful.
 -
 - The problem, I am facing, doesn't have to do with functions
 - but with initialization of statics in template classes.
 -
 - The VC++ bug is as follows:
 -
 - I'm defining the static objects in the template class in the
 - header file itself. This is legal as definitions of
 - tempalte functions, statics, member functions etc could be
 - there in the header file. VC++ 2.x creates problems by
 - defining the statics each time the header is included in
 - different translation units leading to multiple definition
 - error. Now
 - I have to solve the problem such that users of the class (or
 - the header) need not worry about it.
 -
 - Thinking of a workaround made me wish if something like a
 - global macro (or anything else that can be determined at
 - compile-time) were there in the language.
 -
 - eg. If I say
 -
 - #define NO_OF_DEFINITIONS 1
 -
 - in file 1 and want to use this value in some other file
 - (translation unit).
 -
 - Now the question: Can the above effect be achieved by some
 - means and how useful would it be (looks pretty useful to me)?
 -
 - Thanks,
 - Kalyan
 -
 - definition error.


Sounds like the classic multipy-inlcuded header file problem, which is
generally solved like this:

#ifndef _MYHEADER_H_
#define _MYHEADER_H_

.. header file contents

#endif

This definitely eliminates the multiply-defined errors that inclusion can
create for you.

Hope this helps...
Mike.

--
--------------------------------------------------------------------------------
- Mr.H - mike@mrhappy.if.com                         - -------<<<PGP>>>------- -
- All opinions expressed herein are my own... right? - CC BD FD 38 BD C2 FB 56 -
- "Goodbye boys" ... "Have fun storming the castle!" - 02 AA 3C 7A 68 BB AF 54 -
--------------------------------------------------------------------------------





Author: volpe@bart.crd.ge.com (Christopher R. Volpe)
Date: 1995/06/08
Raw View
In article <3r6tr8$41a@natasha.rmii.com>, mike@if.com (MIke Homyack - System Owner) writes:
>On 7 Jun 1995 05:06:51 GMT, Kalyan Kolachala (kal@chromatic.com) wrote:
> - Thanks for all the responses I got. But it seems that in
> - attempting to present the problem in simple and general
> - terms I've made it look like something else. Actually
> - the problem caused by a bug in Visual C++ 2.0/2.1 made
> - me wish there was a feature (TBD later) that could be highly
> - useful.
> -
> - The problem, I am facing, doesn't have to do with functions
> - but with initialization of statics in template classes.
> -
> - The VC++ bug is as follows:
> -
> - I'm defining the static objects in the template class in the
> - header file itself. This is legal as definitions of
> - tempalte functions, statics, member functions etc could be
> - there in the header file. VC++ 2.x creates problems by
> - defining the statics each time the header is included in
> - different translation units leading to multiple definition
> - error. Now
> - I have to solve the problem such that users of the class (or
> - the header) need not worry about it.
> -
> - Thinking of a workaround made me wish if something like a
> - global macro (or anything else that can be determined at
> - compile-time) were there in the language.
> -
> - eg. If I say
> -
> - #define NO_OF_DEFINITIONS 1
> -
> - in file 1 and want to use this value in some other file
> - (translation unit).
> -
> - Now the question: Can the above effect be achieved by some
> - means and how useful would it be (looks pretty useful to me)?
> -
> - Thanks,
> - Kalyan
> -
> - definition error.
>
>
>Sounds like the classic multipy-inlcuded header file problem, which is
>generally solved like this:

No, that's not what it is. You're talking about the problem of including the
same header more than once during the compilation of a single translation
unit. He's talking about having the same header included by multiple
translation units, which ordinarily isn't a problem, except that this header
declares a C++ template which has a static variable associated with it, and
due to a compiler bug, the occurrence of the static variable conflicts with
another occurrence of it in another translation unit, even though statics are
supposed to be local to a translation unit and therefore not conflict.
(Disclaimer: I don't know what quirks C++ templates may through into this
scenario, if any, that may make the rules different from those of C regarding
statics).

--

Chris Volpe    Phone: (518) 387-7766 (Dial Comm 8*833
GE Corporate R&D   Fax:   (518) 387-6560
PO Box 8, Schenectady, NY 12301  Email: volpecr@crd.ge.com






Author: baynes@ukpsshp1.serigate.philips.nl (Stephen Baynes)
Date: 1995/06/08
Raw View
Kalyan Kolachala (kal@chromatic.com) wrote:
: Peter.Kron@corona.com (Peter Kron) wrote:
: >Kalyan Kolachala <kal@chromatic.com> wrote:
--long section on templates which are nothing to do with C so inappropriate
        for comp.std.c removed.
        Note that this was posted to comp.lang.c++,comp.std.c++,comp.std.c
:
: Thinking of a workaround made me wish if something like a
: global macro (or anything else that can be determined at
: compile-time) were there in the language.
:
: eg. If I say
:
: #define NO_OF_DEFINITIONS 1
:
: in file 1 and want to use this value in some other file
: (translation unit).
:
: Now the question: Can the above effect be achieved by some
: means and how useful would it be (looks pretty useful to me)?
:
The standard C mechanism is to put this in a header file and #include the
header file in all the
It will work for C++ too.

Followups to comp.lang.c, comp.lang.c++

--
Stephen Baynes                              baynes@mulsoc2.serigate.philips.nl
Philips Semiconductors Ltd
Southampton                                 My views are my own.
United Kingdom





Author: Kalyan Kolachala <kal@chromatic.com>
Date: 1995/06/07
Raw View
Peter.Kron@corona.com (Peter Kron) wrote:
>Kalyan Kolachala <kal@chromatic.com> wrote:
>
>>
>>The problem I have is something like this.
>>
>>I have a header file with a definition (eg. a function). For
a
>>particular reason (bugs in VC++) I have to have
>>the definition in the header file. But when this
>>header file is included in more than one file I
>>have the multiple definition error.

Thanks for all the responses I got. But it seems that in
attempting to present the problem in simple and general
terms I've made it look like something else. Actually
the problem caused by a bug in Visual C++ 2.0/2.1 made
me wish there was a feature (TBD later) that could be highly
useful.

The problem, I am facing, doesn't have to do with functions
but with initialization of statics in template classes.

The VC++ bug is as follows:

I'm defining the static objects in the template class in the
header file itself. This is legal as definitions of
tempalte functions, statics, member functions etc could be
there in the header file. VC++ 2.x creates problems by
defining the statics each time the header is included in
different translation units leading to multiple definition
error. Now
I have to solve the problem such that users of the class (or
the header) need not worry about it.

Thinking of a workaround made me wish if something like a
global macro (or anything else that can be determined at
compile-time) were there in the language.

eg. If I say

#define NO_OF_DEFINITIONS 1

in file 1 and want to use this value in some other file
(translation unit).

Now the question: Can the above effect be achieved by some
means and how useful would it be (looks pretty useful to me)?

Thanks,
Kalyan

definition error.