Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8985107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:15:37+00:00 2026-06-15T21:15:37+00:00

Very simple question. Is this valid C++11? struct Foo { int bar = 1;

  • 0

Very simple question. Is this valid C++11?

struct Foo {
    int bar = 1;
    int baz = bar;
};

GCC (4.7.2) and Clang (3.1) both accept it with the pedantic settings:

-std=c++11 -Wall -W -pedantic

Intel C++ (13.0.1.117) does not. It barks at int baz = bar; with:

error: a nonstatic member reference must be relative to a specific object

Who is right?

In case you wonder, I use this for code like this, where it brings initialization code closer together, rather than moving the last line into the constructor:

uint8_t colorR = -1;
uint8_t colorG = -1;
uint8_t colorB = -1;
uint8_t colorA = -1;
GLubyte RGBAVec[4] = {colorR, colorG, colorB, colorA};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:15:38+00:00Added an answer on June 15, 2026 at 9:15 pm

    5.1p12 An id-expression that denotes a non-static data member or non-static member function of a class can only
    be used:

    • as part of a class member access (5.2.5) in which the object expression refers to the member’s class
      or a class derived from that class, or
    • to form a pointer to member (5.3.1), or
    • in a mem-initializer for a constructor for that class or for a class derived from that class (12.6.2), or
    • in a brace-or-equal-initializer for a non-static data member of that class or of a class derived from that
      class (12.6.2), or
    • if that id-expression denotes a non-static data member and it appears in an unevaluated operand.

    So yes, this :

    struct Foo {
      int bar = 1;
      int baz = bar;
    };
    

    is valid C++11.

    But be carefull about order because :

    12.6.2p10 In a non-delegating constructor, initialization proceeds in the following order:

    • First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in
      the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes,
      where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
    • Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list
      (regardless of the order of the mem-initializers).
    • Then, non-static data members are initialized in the order they were declared in the class definition
      (again regardless of the order of the mem-initializers).
    • Finally, the compound-statement of the constructor body is executed

    So as specified in the Non-static data member initializers proposal (Problem 3) :

    A third issue is that class-scope lookup could turn a compile-time error into a run-time error:

    struct S {
        int i = j; // ill-formed without forward lookup, undefined behavior with
        int j = 3;
    };
    

    (Unless caught by the compiler, i might be intialized with the undefined value of j.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a very simple question, I apologise - I'm new to rails.
This is probably a very simple question so please forgive my ignorance, but can
This is probably a very simple question but I'm a little confused how this
This is another probably very simple question, but I haven't been able to find
This is a very simple question for compiler guys, but I always get confused
This is a very simple question, but I can't seem to find something about
this may be a very simple question, but is it possible to force a
This is a very simple question and I feel stupid for asking it, but
This may be very simple question,But please help me. i wanted to know what
This should be very simple question. There are many programming languages out there, compiled

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.