n1794 (6 pages)

Previewing pages 1, 2 of 6 page document View the full content.
View Full Document

n1794



Previewing pages 1, 2 of actual document.

View the full content.
View Full Document
View Full Document

18 views

Unformatted text preview:

Deducing the type of variable from its initializer expression revision 2 Programming Language C Document no N1794 05 0054 Jaakko J rvi Texas A M University College Station TX jarvi cs tamu edu Bjarne Stroustrup AT T Research and Texas A M University bs research att com Gabriel Dos Reis Texas A M University College Station TX gdr cs tamu edu 2005 04 13 1 Introduction This document is a minor revision of the document N1721 04 0161 The document N1721 04 0161 contained the suggested wording for new uses of keyword auto which were unanimously approved by the evolution group meeting in 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 single statement N1721 04 0161 allowed only one variable initialization per statement 2 Proposed features We suggest that the auto keyword would indicate that the type of a variable is to be deduced from its initializer expression For example auto x 3 14 x has type double The auto keyword can occur as a basic type specifier allow to be used with cv qualifiers and and the semantics of auto should follow exactly the rules of template argument deduction Examples the notation x T is read as x has type T int foo auto x1 foo x1 int const auto x2 foo x2 const int auto x3 foo x3 int error cannot bind a reference to a temporary float bar auto y1 bar y1 float const auto y2 bar y2 const float auto y3 bar y3 float A fii 1 Doc no N1794 05 0054 auto z1 fii auto z2 fii auto z3 bar z1 A z2 A error bar does not return a pointer type auto z4 fii z4 A 2 A major concern in discussions of auto like features has been the potential difficulty in figuring out whether the declared variable will be of a reference type or not Particularly is unintentional aliasing or slicing of objects likely For example class 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 For example A foo A bar A x1 foo auto x1 foo x1 A x1 A A x2 foo error we cannot bind a non lvalue to a non const reference auto x2 foo error A y1 bar auto y1 bar y1 A y1 A A 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 through consistent use of Multi variable declarations More 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 can get different types as is the case in the above example The requirement on the type deductions to lead to the same type is best explained by translation to template argument deduction The deductions in the above example correspond to 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 3 Direct initialization syntax Direct initialization syntax is allowed and is equivalent to copy initialization For example auto x 1 x int auto x 1 x int The semantics of a direct initialization expression of the form T v x with T a type expression containing an occurrence of of auto v as a variable name and x an expression is defined as a translation to the corresponding copy initialization expression 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 with an 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 wording Section 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 with any other type specifier in which case it is treated as a storage class specifier If the decl specifiersequence contains no type specifier other than auto then the following restrictions apply to the decl specifier sequence It must be followed by one or more init declarators each of which must have a non empty initializer 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 specifier auto 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 auto To Table 7 add the line auto placeholder for a type Doc no N1794 05 0054 4 Section 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 declare an object and to specify an initial value the type of the declared identifier is deduced from the type of its initializer dcl auto Replace paragraph 4 with First the decl specifier seq determines a type or when it contains an occurrence of auto a type scheme A type scheme yields a type when the occurrence of auto in the type scheme is replaced by a type In a declaration T D the decl specifier seq T determines the type or type scheme T Example in the declarations int unsigned i const auto p f the type specifiers int unsigned determine the type unsigned int and the type specifier const auto determines the type scheme const auto dcl type simple Section 8 3 1 Pointers dcl ptr Change the first paragraph to In a declaration T D where D has the form cv qualifier seqopt D1 and the type or type scheme of the identifier in the declaration T D1 is derived declarator type list T then the type or type scheme of the identifier of D is derived declarator type list cv qualifier seq pointer to T The cv qualifiers apply to the pointer and not to the object pointed to The …


Access the best Study Guides, Lecture Notes and Practice Exams

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 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?