Jan 26, 2021

[C++17] something about attribute

[[nodiscard]]

Should usually be used to signal misbehavior when return values are not used. The misbehavior might be:

• Memory leaks, such as not using returned allocated memory.

• Unexpected or non-intuitive behavior such as getting different/unexpected behavior when not using the return value.

• Unnecessary overhead, such as calling something that is a no-op if the return value is not used.


[[maybe_unused]]

Can be used to avoid warnings by the compiler for not using a name or entity.


[[fallthrough]]

Can be used to avoid warnings by the compiler for not having a break statement after a sequence of one or more case labels inside a switch statement.


General Attribute Extensions:

  1. Attributes can now be used to mark namespaces.
    namespace [[deprecated]] DraftAPI {}
    
  2. Attributes can now be used to mark enumerators (values of enumeration types).
  3. For user-defined attributes, which should usually be defined in their own namespace, you can now use a using prefix to avoid the repetition of the attribute namespace for each attribute. That is, instead of: 
    [[MyLib::WebService, MyLib::RestService, MyLib::doc("html")]] void foo();
    

you can just write

[[using MyLib: WebService, RestService, doc("html")]] void foo();


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.