• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: In-class initialization of integral static const variables & Linker errors
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: In-class initialization of integral static const variables & Linker errors


  • Subject: Re: In-class initialization of integral static const variables & Linker errors
  • From: Guillaume Billard <email@hidden>
  • Date: Thu, 16 Jul 2009 16:50:37 +0200


Le 16 juil. 09 à 15:31, Eric A. Borisch a écrit :

This (it working when placed in main()) is likely due to an optimization in the compiler removing references to Foo:foo with their (const) value during the compilation phase.

On my g++, it works with -O1, but not with -O0. icpc (intel's c++) works either way...

$ g++ -c main.cpp -o main.o -O0  && nm main.o | c++filt 
         U Bar::bar
         U Foo::foo
00000000 T _main
00000000 A _main.eh

$ g++ -c main.cpp -o main.o -O1  && nm main.o | c++filt 
00000000 T _main
00000000 A _main.eh

Some other discussion of static member initialization:
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr038.htm

- Eric

On Thu, Jul 16, 2009 at 2:47 AM, Guillaume Billard<email@hidden> wrote:
>
> Le 16 juil. 09 à 04:08, Ethan Tira-Thompson a écrit :
>
>>>> Trying to build this with Xcode 3.1.3 on Mac OS X 10.5.7, I get the
>>>> following linker errors:
>>>> Undefined symbols:
>>
>> You are getting this because you still need to define the storage for the
>> statics.  Sometimes this isn't necessary, and I'm not sure why. But you can
>> put 'int const Foo::foo; int const Bar::bar;' somewhere in a .cpp file, and
>> the errors should go away.
>>
>>
>>> I think initializing const variables in the class declaration, if it
>>> is allowed by the ISO standard at all, is not supported by all
>>> compilers.
>>
>> I'm pretty sure it's in the standard that 'int' statics can be initialized
>> within the class declaration (in the header).
>> However no other types can be.  I'm not sure why they do that
>> differentiation.
>>
>> -Ethan
>>
>
> What I don't understand is that if the main() code is replaced with the
> following, it builds fine:
>        int i = Foo::foo;
>        int j = Bar::bar;
>
>        return (argc == 1) ? i : j;
>
> I indeed defined the variables out-of-class as a fix, but I'm interested if
> someone knows when this is necessary and when it's not.

I think I confused initialization and definition.
According to the standard (9.4.2) :

If a static data member is of const integral or const enumeration type, its declaration in the class definition can  specify a constant-initializer which shall be an integral constant _expression_ (5.19). In that case, the member can appear  in integral constant expressions. The member shall still be defined in a namespace scope if it is used in the program and  the namespace scope definition shall not contain an initializer.

So it appears that the error in the first case is valid:

return (argc == 1) ? Foo::foo : Bar::bar;

And that there should be an error in the second case as well:

int i = Foo::foo;
int j = Bar::bar;

return (argc == 1) ? Foo::foo : Bar::bar;

As advised in previous answers, the right fix is to define the variables out of the classes:

class Foo
{
public:
static int const foo = 0;
};

class Bar
{
public:
static int const bar = 0;
};

int const Foo::foo;
int const Bar::bar;

int main(int argc, char**)
{
return (argc == 1) ? Foo::foo : Bar::bar;
}

Thanks for all the answers, please enlighten me if I'm still mistaken.


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >In-class initialization of integral static const variables & Linker errors (From: Guillaume Billard <email@hidden>)
 >Re: In-class initialization of integral static const variables & Linker errors (From: Michael Crawford <email@hidden>)
 >Re: In-class initialization of integral static const variables & Linker errors (From: Ethan Tira-Thompson <email@hidden>)
 >Re: In-class initialization of integral static const variables & Linker errors (From: Guillaume Billard <email@hidden>)

  • Prev by Date: Re: GDB problems and version
  • Next by Date: Xcode - View code files in fullscreen
  • Previous by thread: Re: In-class initialization of integral static const variables & Linker errors
  • Next by thread: Fortify & Xcode
  • Index(es):
    • Date
    • Thread