Proposal of [[unused]], [[nodiscard]] and [[fallthrough]] attributes.    Document No.: Dnnnn  Project: Programming Language C++ ­ Evolution  Author: Andrew Tomazos <​ [email protected]​ >  Date: 2015­09­03   

Background    There are currently 3 standard attributes in C++.  They are:      [[noreturn]]    [[carries_dependency]]    [[deprecated]]    They are specified, respectively, in [dcl.attr.noreturn], [dcl.attr.depend] and [dcl.attr.deprecated].    In addition to the standard attributes, most implementations offers a set of non­standard attributes.   

Proposal    We propose the standardization of 3 more attributes.  They are based on some of the most common and  useful existing non­standard attributes.  They are      [[unused]]    [[nodiscard]]    [[fallthrough]]    We offer a rough informal description here that mixes the normative semantics and the practical usage.  (For  a formal description of what is proposed, see the Wording section below).    [[unused]] means that the entity to which it appertains may appear intentionally unused for some reason.  If  an implementation would have warned about the entity being unused, the [[unused]] attribute suppresses  the warning.    [[nodiscard]] means that:    ­ when appertaining to a function, the return value is important to its proper use.  If a function call  expression to that function has its return value discarded a warning should be issued.  ­ when appertaining to a type, it is is as if all functions returning that type are marked [nodiscard]].    The [[fallthrough]] attribute is used like a statement we call a ​ fallthrough statement:​         [[fallthrough]];    A fallthrough statement is placed just before a case label in a switch.  It serves as a hint that the feature of  execution “falling through” into the proceeding case­labelled statement is intentional (not accidental). 

 

Motivation    The three proposed attributes share a common theme of all being about capturing the intent of the  programmer to improve the accuracy of diagnostic generation.    In many cases an unused entity can be a symptom of a logic error.  For this reason, most implementations  can be configured to issue warnings about certain kinds of entities when they are not sufficiently used (by  some implementation­defined definition).  Such a diagnostic is usually part of the default set (­Wall).  In  cases where such a warning is emitted but determined to be a false alarm, some technique needs to be  used to individually suppress the warning.  There are a variety of non­standard techniques:      __attribute__((“unused”))     [[gnu::unused]]    __pragma(warning(suppress:4100))    #pragma foo diagnostic unused­bar ignored    void f(int /*arg*/);    #define UNUSED(x)  (void)(x)    void f(int /*unused*/);    template  void Unused(T&&) {}    #ifdef COMPILER1       #define UNUSED something    #elseif COMPILE2       #define UNUSED something else    etc    By standardizing the [[unused]] attribute we would provide a common, portable and more readable way to  express this intent.    A value­returning function can be called purely for its side­effects, and the return value discarded.  For some  functions, the return value is an essential component for its proper operation, and should not be discarded.  Some types are designed to be used as the return value of such functions.  It is a common logic error to  accidentally discard a return value of such a function or of such a type.  Similar to [[unused]], there are a  variety of non­standard techniques to express such a property, such as [[gnu::warn_unused_result]], which  provokes a warning when such a return value is discarded.  The [[nodiscard]] attribute allows this intent to  be expressed in a common, portable way.    An extremely expensive logic error is accidental use of the little­used feature of switch statements whereby  the path of execution can implicitly flow from one case block to the one proceeding it.  The logic error that  ensues from such accidental fall through is usually quite insidious because the side effects of executing a  second “wrong” case block generally appear as quite reasonable things to happen locally without  considering the big picture of the system architecture.  The system misbehaviour is usually caught some  distance from the origin, and can take a long­time to diagnose.  For this reason, many programming  languages offer a fallthrough statement to explicitly denote this intent, and will fail to compile if it is not used  on such an execution path.  Some C++ implementations are have developed a popular diagnostic and  accompanying [[fallthrough]] attribute statement.  We propose the standardization of the attribute to provide  the feature in a common portable way to C++. 

 

Examples    Example 1    Compiled with an unused variables warning enabled:    #define NDEBUG    std::pair flatten(int x, int y, int z) {    // WARNING: y unused    assert(y == 0);     return {x, z};  }    std::pair flatten(int x, int y [[unused]], int z) {    // OK    assert(y == 0);    return {x, z};  }    Example 2    // Attempt to write up to len bytes from buf.  // Returns: Number of bytes successfully written.  [[nodiscard]] int write(void* buf, int len);    int main() {    write(“hello”, 5); // WARNING: return value of nodiscard function discarded.  }    Example 3    Compiled with an implementation­defined implicit fallthrough warning enabled:    switch (n) {  case 22:  case 33:  // OK: no statements between case labels    f();  case 44:  // WARNING: no fallthrough statement    g();    [[fallthrough]];  case 55:  // OK    if (x) {      h();      break;    } 

  else {      i();      [[fallthrough]];    }  case 66:  // OK    p();    [[fallthrough]]; // WARNING: fallthrough statement does not directly precede  case label    q();  case 77:  // WARNING: no fallthrough statement    r();  }   

Existing Practice    There are literally millions of examples of the non­standard attributes on which [[unused]], [[nodiscard]] and  [[fallthrough]] are based (and of the families of related non­standard techniques) being used in the wild.  One  must merely perform a GitHub code search at github.com/search, or an OpenHUB code.openhub.net search  to see these.   

Design Philosophy    As is common practice thus far, all of the proposed attributes can be completely ignored by an  implementation, and that implementation remains in conformance.    We are not proposing standardization or normalization of “warnings”.  As per the existing attributes, all three  proposed attributes merely serve as possible hints to an implementation, with a little non­normative  guidance on what they intend.  There is nothing novel in their proposal, and they are mainly non­prescriptive  standardization of extensive existing practices.    We have intentionally erred on the side of implementation freedom.  We can always further refine the  standard semantics in a later proposal if need be.   

Wording    7.6.6 Unused attribute    [dcl.attr.unused]    1. The attribute­token unused can be used to mark various names, entities and expression statements  that may be intentionally not used.   [Note: If an implementation would have otherwise emitted a  warning about an entity, so marked, not being used, they are encouraged not to. Implementations  are discouraged from emitting a warning if an entity, so marked, is used. ­­ end note]  It shall appear  at most once in each attribute­list, with no attribute­argument­clause.  2. The attribute may be applied to the declaration of a class, a typedef­name, a variable, a non­static  data member, a function, an enumeration, a template specialization, or a non­null expression  statement. 

3.

When applied to an expression statement, unused indicates the expression is intentionally a  discarded­value expression. [Note: If an implementation would have otherwise emitted a warning  due to a nodiscard attribute, they are encouraged not to. ­­ end note] 

  7.6.7 Nodiscard attribute [dcl.attr.nodiscard]    1. The attribute­token nodiscard can be used to mark a function, function template specialization or  type.  It shall appear at most once in each attribute­list, with no attribute­argument­clause.  2. If a function call expression to a function (or instantiation of a function template specialization)  marked with nodiscard appears as a discarded­value expression statement, the program is  ill­formed, no diagnostic required, unless:    ­ The marked entity is a type and the expression statement is an assignment or compound  assignment, or  ­ The expression statement is marked [[unused]].    7.6.8 Fallthrough attribute [dcl.attr.fallthrough]    1. A null statement marked with the attribute­token fallthrough, is a ​ fallthrough statement​ .   The  fallthrough attribute­token shall appear at most once in each attribute­list, with no  attribute­argument­clause.  2. A fallthrough statement may appear in an enclosing switch statement, on a path of execution  immediately between a proceeding non­jump statement and a succeeding case label.  3. [Note:  If an implementation would have otherwise issued a warning about implicit fallthrough on that  path of execution, they are encouraged not to. ­­ end note]   

Proposal of [[unused]], [[nodiscard]] and [[fallthrough]] -

Sep 3, 2015 - must merely perform a GitHub code search at github.com/search, or an OpenHUB code.openhub.net search to see these. Design Philosophy.

163KB Sizes 0 Downloads 113 Views

Recommend Documents

Disposal of unused medication and sharps for North Andover ...
Disposal of unused medication and sharps for North Andover residents.pdf. Disposal of unused medication and sharps for North Andover residents.pdf. Open.

Disposal of unused medication and sharps for North ... - Drive
Disposal of unused medication and sharps for North Andover residents.pdf. Disposal of unused medication and sharps for North Andover residents.pdf. Open.

Offer All Unused Auto Immediately And Also Clean The Place.pdf ...
... the fees that are maximum appealing. As they would give one. of the valuable cost for requesting them ahead and car removals Brisbane, you never want to. really go and approach them a phone call is more than enough. • They would bring a vehicle

Panel Proposal
Choose an option. ( ) Member of SAAS ( ) Member of ASA ( ) Processing Membership. Title of Proposed panel: Panel Abstract (200-300 words): Please, complete this form and send it, in electronic format (via e-mail), to board members. Rodrigo Andrés (r

Table of Contents and Project Proposal - Worcester Polytechnic Institute
May 28, 2012 - Programming Project: . ..... This is only the basics of how the markets and trade works. For ...... bollingeronbollingerbands.com/chart/main.php>.

Proposal of File String Literals -
string literal, except the content of the string is stored in a separate dedicated source file, rather than inline in the host source file. The path of the file is specified, ...

A PROPOSAL FOR COMMUNITY DRIVEN AND ... - GitHub
Dec 4, 2012 - 1. INTRODUCTION. Astronomical catalogues and databases are almost as old as ... In recent years, the internet allowed the open source com- munity to make ..... and tools that the computer science and open source commu- nity uses. .... d

A proposal of charact
The term has been adopted by early computer graphics for indicating the goal of accurately ..... Virtual reality devices and computer sciences have impressively developed ... London and Edinburgh ... Oxford: Oxford University Press. Gibson ...

jiucqu Google Play Gift Card Codes Unused Scholarships
Online Google Play Code Generator Get Free Google Play Codes. Google Play Gift Cards; ... What is a PlayStation Network Gift Card? (PSN Gift Card) .

Unused Psn Codes List September 2017 750
2017 Kraft Live Free Game Generator Codes on Android phone, Code Generator Psn ... Ads Video Games vs Code Generator Psn Generator Online No Human ...

Spotify Gift Card Codes List Unused 590
Apr 30, 2017 - Connect with EarthLink, the award-winning Internet service . ... April 2017 Real Truck Coupon Codes ... delivery via email from Real Truck. ... play on tonight, The TV Channels broadcast Code Generator Spotify Gift Card.

Google Play Gift Card Codes Unused Band 700
2017 Kohls Illinois, Inc., Kohls and Kohls brand names are trademarks of Kohls Illinois, Inc. Android, Google Play and the Google Play logo are ... Digital Codes ...

Approval of Proposal Defense Committee.pdf
Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Approval of Proposal Defense Committee.pdf. Approval of Proposal Defense Committee.pdf. Open. Extract. Open with. Sign In. Main menu. Whoops! There was a

Unused Psn Codes List 2017 822
The official PlayStationStore - Buy the latest PlayStation games, movies and TV ... Generator Free Android Apk score, Free Game Generator Codes Psn Code ...

Amazon Unused Gift Card Code List 414
The Army Catering Corps We Sustain Unofficial Site ... Free Game Generator Codes Amazon Gift Card Numbers Code Generator Free Amazon Gift Card Codes ...

PROPOSAL SOLICITATION: PATIENT SAFETY CENTERS OF INQUIRY
to compete to become a Patient Safety Center of Inquiry (PSCI). Patient Safety .... 2016; however, the call for PSCI travel requests is unlikely to be released prior ...

Structure of MBA Thesis Research Proposal
Hypotheses are relatively easy to test if your research study is more or less quantitative in nature and if you wish ... What are the best practices in the world and how do they relate to your research issue? ... main data analysis methods and why yo

CITY OF MOBILE, ALABAMA REQUEST FOR PROPOSAL ...
Apr 30, 2016 - State company name and all contact information including the name, ... and became disabled prior to age 19 while covered by the City of.

Free Psn Codes List Unused 2017 185
PlayStation Network (PSN) is a digital media entertainment service provided by .... Generator Codes on Android phone, Free Game Generator Codes Free Psn.

riolha Google Play Gift Card Codes Unused 2017 ...
riolha Google Play Gift Card Codes Unused 2017 Printable Calendar ... of Denver, Colorado, provides 3D Computer Aided Design and support services.

SASE-Submitting-a-Proposal
Log into your account at sase.org – green button “sign in” in the top right-hand corner of the homepage: ... Once you are logged in, you will see the green button “submit a paper” in the top right-hand corner of any page on the ... Conferen

venue proposal -
Coke and Still Mineral Water. Unlimited and bottled. 1.5 Pricing. Original Menu (Less the lamb and chicken karahi, veg curry and naan) is £15.95 per head.

Request for Proposal - Ning
Sep 3, 2013 - Synopsis: Enhancing Mobile Populations' Access to HIV and AIDS Services, Information and. Support a 5 year project funded by Big Lottery ...

proposal pdf.pdf
Page 1 of 6. UAL Awarding Body – Foundation. Unit 7- Project Proposal. Candidate. Number. Candidate. Number. Candidate Name Odysseus Miltiadous. Candidate. Number. 96519217. Pathway Graphic Design. Project Title Can we rely on the government to act