DOC PREVIEW
n1794

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

IntroductionProposed featuresProposed wordingAcknowledgmentsDeducing the type of variable from its initializer expression(revision 2)Programming Language C++Document no: N1794=05-0054Jaakko JärviTexas A&M UniversityCollege Station, [email protected] StroustrupAT&T Researchand Texas A&M [email protected] Dos ReisTexas A&M UniversityCollege Station, [email protected] IntroductionThis document is a minor revision of the document N1721=04-0161. The document N1721=04-0161 contained thesuggested wording for new uses of keyword auto, which were unanimously approved by the evolution group meetingin Redmond, October 2004. Based on the discussions and straw-polls in the Lillehammer meeting in April 2005,this document now adds wording for allowing the initialization (with auto) of more than one variables in a singlestatement; N1721=04-0161 allowed only one variable initialization per statement.2 Proposed featuresWe suggest that the auto keyword would indicate that the type of a variable is to be deduced from its initializerexpression. For example:auto x = 3.14; // x has type doubleThe auto keyword can occur as a basic type specifier (allow to be used with cv-qualifiers, ∗, [] and &) and the semanticsof auto should follow exactly the rules of template argument deduction. Examples (the notation x : T is read as “x hastype T”):int foo();auto x1 = foo(); // x1 : intconst auto& x2 = foo(); // x2 : const int&auto& x3 = foo(); // x3 : int&: error, cannot bind a reference to a temporaryfloat& bar();auto y1 = bar(); // y1 : floatconst auto& y2 = bar(); // y2 : const float&auto& y3 = bar(); // y3 : float&A∗ fii()1Doc. no: N1794=05-0054 2auto∗ z1 = fii(); // z1 : A∗auto z2 = fii(); // z2 : A∗auto∗ z3 = bar(); // error, bar does not return a pointer typeauto z4[] = fii(); // z4 : A∗A major concern in discussions of auto-like features has been the potential difficulty in figuring out whether thedeclared variable will be of a reference type or not. Particularly, is unintentional aliasing or slicing of objects likely?For exampleclass B { ... virtual void f(); }class D : public B { ... void f(); }B∗ d = new D();...auto b = ∗d; // is this casting a reference to a base or slicing an object?b.f(); // is polymorphic behavior preserved?Basing auto on template argument deduction rules provides a natural way for a programmer to express his intention.Controlling copying and referencing is essentially the same as with variables whose types are declared explicitly. Forexample:A foo();A& bar();...A x1 = foo(); // x1 : Aauto x1 = foo(); // x1 : AA& x2 = foo(); // error, we cannot bind a non−lvalue to a non−const referenceauto& x2 = foo(); // errorA y1 = bar(); // y1 : Aauto y1 = bar(); // y1 : AA& y2 = bar(); // y2 : A&auto& y2 = bar(); // y2 : A&Thus, as in the rest of the language, value semantics is the default, and reference semantics is provided throughconsistent use of &.Multi-variable declarationsMore than one variable can be declared in a single statement:int i;auto a = 1, ∗b = &i;In the case of two or more variables, both deductions must lead to the same type. Note that the declared variables canget different types, as is the case in the above example. The requirement on the type deductions to lead to the sametype is best explained by translation to template argument deduction. The deductions in the above example correspondto the deductions of template parameter T below:template <class T>void foo(T a, T∗ b);...foo(1, &i);Here, T must be deduced to be the same type based on both arguments; otherwise the code is ill-defined.Doc. no: N1794=05-0054 3Direct initialization syntaxDirect initialization syntax is allowed and is equivalent to copy initialization. For example:auto x = 1; // x : intauto x(1); // x : intThe semantics of a direct-initialization expression of the form T v(x) with T a type expression containing an occurrenceof of auto, v as a variable name, and x an expression, is defined as a translation to the corresponding copy initializationexpression T v = x. Examples:const auto& y(x) −> const auto& y = x;It follows that the direct initialization syntax is allowed with new expressions as well:new auto(1);The expression auto(1) has type int, and thus new auto(1) has type int∗. Combining a new expression using auto withan auto variable declaration gives:auto∗ x = new auto(1);Here, new auto(1) has type int∗, which will be the type of x too.3 Proposed wordingSection 7.1.5.1 Type specifiers [dcl.type.simple]Add to the paragraph 1— auto can either be a storage class specifier, or a simple type specifier. auto can be combined withany other type specifier, in which case it is treated as a storage class specifier. If the decl-specifier-sequence contains no type specifier other than auto, then the following restrictions apply to thedecl-specifier-sequence:– It must be followed by one or more init-declarators, each of which must have a non-emptyinitializer of either of the following two forms:= initializer−clause( initializer−clause )– The only other allowed decl-specifiers are cv-qualifiers and the storage class specifier static.[Example: The following are valid declarations:auto x = 5;const auto *v = &x, u = 6;static auto y = 0.0;static auto int z; // invalid, auto treated as a storage class specifierauto int r; // ok— end example]Section 7.1.5.2 Simple type specifiers [dcl.type.simple]In paragraph 1, add the following to the list of simple type specifiers:autoTo Table 7, add the line:auto placeholder for a typeDoc. no: N1794=05-0054 4Section 8.3 Meaning of declarators [dcl.meaning]New paragraph after paragraph 1:If decl-specifier-sequence contains the simple-type-specifier auto, the declarator is required to declarean object and to specify an initial value; the type of the declared identifier is deduced from the type of itsinitializer ([dcl.auto]).Replace paragraph 4 with:First, the decl-specifier-seq determines a type; or, when it contains an occurrence of auto, atype scheme. A type scheme yields a type when the occurrence of auto in the type scheme is replacedby a type. In a declarationT Dthe decl-specifier-seq T determines the type, or type scheme, “T”. [Example: in the declarationsint unsigned i;const auto& p = f();the type specifiers int unsigned determine the type “unsigned int”, and the type specifierconst auto determines the type scheme “const auto” ([dcl.type.simple]).]Section 8.3.1 Pointers


n1794

Download n1794
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view n1794 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view n1794 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?