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

  • Home
  • SEARCH
  • 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 8190861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:46:24+00:00 2026-06-07T03:46:24+00:00

Does C++11 allow declaring non-static data members as ‘auto’ if they are initialized in

  • 0

Does C++11 allow declaring non-static data members as ‘auto’ if they are initialized in the declaration? For example:

struct S
{
    auto x = 5;  // in place of 'int x = 5;', which is definitely allowed
};

GCC 4.7 rejects the above code, while it accepts int x = 5;.

Assuming this is not a compiler bug but rather the standard really doesn’t allow it, why not? It would be just as useful as declaring local variables auto.

  • 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-07T03:46:25+00:00Added an answer on June 7, 2026 at 3:46 am

    The rule for prohibiting non-static members is in 7.1.6.4 clause 4:

    The auto type-specifier can also be used in declaring a variable in
    the condition of a selection statement (6.4) or an iteration statement
    (6.5), in the type-specifier-seq in the new-type-id or type-id of a
    new-expression (5.3.4), in a for-range-declaration, and in declaring a
    static data member with a brace-or-equal-initializer that appears within the member-specification of a class definition (9.4.2).

    I found the rationale for it being static here which reflects how James McNellis explains it in the comment.

    One national body dislikes allowing the auto type-specifier for
    non-statics. From an e-mail to the authors:

        template< class T >
        struct MyType : T {
          auto data = func();
          static const size_t erm = sizeof(data);
        };
    

    In order to determine the layout of X, we now have 2-phase name lookup and ADL. Note that func could be either a type or a function;
    it may be found in T, the namespace of MyType, the associated
    namespace(s) of T when instantiated, the global namespace, an
    anonymous namespace, or any namespaces subject to a using directive.
    With care we could probably throw some concept_map lookup in for luck.
    Depending on the order of header inclusion I might even get different results for ADL, and break the One Definition Rule – which
    is not required to be diagnosed.

    Because of this controversy, the authors no longer propose that auto
    be allowed for non-static data members.

    So, basically depending on the order of header inclusion, the type of data could be very different. Of course, auto x = 5; would not need to depend on 2-phase name lookup or ADL, however, I’m a assuming that they made it a “blanket” rule because otherwise, they would have to make individual rules for every use case which would make things very complicated.

    In the same paper, the author proposes eliminating this restriction, however, it seems this proposal has been rejected probably due to the above rationale and also so that expected behavior can be the same no matter what the initializer is.

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

Sidebar

Related Questions

While ruby does allow modifying any class at any place in the code, I
Does C# allow hashtables to be populated in one-line expressions? I am thinking of
IE does not allow writing to the innerHTML property of style or head elements.
C++ does not allow polymorphism for methods based on their return type. However, when
Facebook does not allow callbacks on ip addresses. So how does one test their
Problem: does not allow me to renderBinary . The method renderBinary(InputStream) in the type
Why does gcc allow extern declarations of type void? Is this an extension or
Firefox 3.5 does not allow creating java OBJECT tag with Javascript (document.write)? this technique
The organization policy does not allow normal users be assigned to Administrators group of
Our websites article backend does not allow access to anything above </head> which prevents

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.