• 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
GCC issue: No error on in-class initialization of non-integral static const variables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

GCC issue: No error on in-class initialization of non-integral static const variables


  • Subject: GCC issue: No error on in-class initialization of non-integral static const variables
  • From: Zachary Pincus <email@hidden>
  • Date: Sun, 10 Jul 2005 22:35:56 -0700

According to the C++ standard (I think! Correct if wrong.), in-class initialization of static const integral types is permitted.

That is:
struct Foo {
  static const int bar = 10;
};

is ok, whereas if 'bar' were not an integral type (enum, int, etc.) but a float or pointer or so forth, it would not be permitted.

Now, previous versions of GCC didn't mind when 'bar' was a float, etc. But with GCC's push to better standards compliance, this is no longer allowed.

The only problem is that GCC doesn't produce an error about this. It's only at link-time (or worse, run-time, if '-undefined dynamic_lookup' is used) that this causes an 'undefined symbol' error.

For example, the following will compile fine, but not link:

struct Foo {
  static const float bar = 10;
  void baz() {
    float f = bar;
  }
};

int main(int argc, char* argv[] ) {
    Foo f;
  f.baz();
}

Clearly (to me), GCC should be issuing an error about line 2 instead of silently failing to generate a symbol for it. Line 2 seems to be more an error of conformance to the C++ language (the compiler's job to catch), rather than an error about storage locations (the linker's job to catch). After all, GCC properly produces errors about other types of improper in-class initializations. It's just non-integral static const's that it misses.

Is this a problem in main-line GCC, or Apple's GCC? If the former, I'll file a bug there.

I'm mostly bringing this up here because (a) it might be only in Apple's GCC, and (b) it has particularly dastardly effects on OS X where the 'dynamic_lookup' linker option can allow these semantic errors to lie dormant for quite some time.

Zach Pincus

Program in Biomedical Informatics and Department of Biochemistry
Stanford University School of Medicine

_______________________________________________
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


  • Follow-Ups:
    • Re: GCC issue: No error on in-class initialization of non-integral static const variables
      • From: "Justin C. Walker" <email@hidden>
  • Prev by Date: Re: Attach a file to a mail
  • Next by Date: Re: GCC issue: No error on in-class initialization of non-integral static const variables
  • Previous by thread: Re: Attach a file to a mail
  • Next by thread: Re: GCC issue: No error on in-class initialization of non-integral static const variables
  • Index(es):
    • Date
    • Thread