Doc No: N2713 = 08-0223
Date: 2008-08-04
Reply to:  Bill Seymour <stdbill.h@pobox.com>

Allow auto for non-static data members

Bill Seymour


Rationale

An early version of the non-static data member initializer paper deleted “static” from 7.1.6.4p4, which lists the places where the auto type-specifier may appear.

One national body disliked allowing non-statics to be declared auto. From an e-mail to the author:

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 current version of the initializer paper, N2712, retains the “static”.

Recently, it was pointed out on comp.lang.c++.moderated that one can get the same effect anyway, just with uglier code, using decltype. Because of that, the author believes that the objection to auto has softened.

This paper suggests deleting the “static”. If N2712 is not accepted for C++0X, this paper is moot.


Suggested change to the Working Paper, N2691 as modified by N2712

7.1.6.4 auto specifier [dcl.spec.auto]

Delete “static” from paragraph 4:

The auto type-specifier can also be used in declaring an object in the condition of a selection statement (6.4) or an iteration statement (6.5), in the type-specifier-seq in a new-type-id (5.3.4), 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).