What are the syntax specifications (keyword / context binding) for C ++ function signatures?

The richness of expressiveness and syntax of C ++ makes it somewhat difficult to use several functions of the language on one signature.

For example, I would like to declare the member function [1] as:

  • virtual

  • override

    // example of the context keyword
  • noexcept

  • const

  • having a return type

This gets more complicated when you consider that functions can also be

  • default
  • cross out
  • final (for virtual methods)
  • Templates
  • Explicit specializations
  • [[deprecated]]

  • ... and many others that I don't know / suspect right now

The standard wording looks Greek to me, and even that was not true, I cannot figure out what is connected with it (or that a syntax error like [1] might even be illegal).

I know reading the individual functions (overrides, noexcept, etc.) will help, but is there a guide to using them in coaction? ( syntactically . don't expect / want a response to the functionality of the functions)

EDIT

Just in case it helps, I think the excerpt will be transcribed as follows

8.4.1 Function definitions are

function-definition : attribute-speci-fi-seqopt decl-spec-fi-seqopt declarator virt-speci-fi-seqopt function-body

+3


source to share


1 answer


If you have a copy of the spec, you can search for the grammar in each section. It sounds like you want complete guidance as to what is and what is wrong, and the spec is the only real source of that information. Fortunately, it's not that hard to read, it's just time consuming and tedious.

Here is the grammar for defining functions, taken from the beginning of section 8.4.1 of N3690. I suggest reading the primer for context-free grammars first ( Wikipedia - go to examples). Note that everyone has their own choices about how they write context-free grammars, so the grammar in the C ++ specification will be slightly different from what you see in, say, Wikipedia or the compiler tutorial.

function-definition:
    attribute-specifier-seqopt decl-specifier-seqopt declarator virt-specifier-seqopt function-body
function-body:
    ctor-initializeropt compound-statement
    function-try-block
    = default;
    = delete;

Taking this into account, we see that the "function body" has four alternatives: one on each line. The two alternatives = delete

and = default

, so they go in the same place as the more interesting function body. (The try block of a function is a slightly obscure way of defining a function in C ++, so let's just ignore it.)

For "function-definition" you can see that it has one line, so there is only one way to define the function. Each element on the line must appear in order. Parts with an "opt" index are optional. Thus,

  • Attributes first
  • The following declaration specifiers (eg, static

    , int

    , extern

    ...)
  • Announcement after that (for example function_name(int x, int y) -> int

    )
  • After that the virtual specifier ( override

    or final

    )
  • Function body last (this includes normal function bodies as { ... }

    well as = delete

    and = default

    )

Once you have a little CFG reading practice, it becomes second nature. The only tricky part is jumping around the C ++ spec to find all the definitions you need.

Notes



Also note that only the syntax will be displayed in the grammar. If you just read the grammar, you write nonsense like:

// permissible, according to the grammar
extern static const void long short int int x;

      

Reading the text will explain what you cannot, for example, have short long

, even if grammar allows it.

Another trick is to find the definition you are looking for. As Certy asks, how do you define pure functions? Where is it going = 0

? Well, you may find what = 0

appears in section 9.2 as pure-specifier

, but can only appear on member-declarator

. Therefore, it cannot appear in function-definition

, it can only appear in an "announcement". The syntax for declaration and definition is slightly different.

In other words, you cannot define a pure function, but you can declare a pure function.

Further reading

If you are interested in this topic, I recommend Compiled: Principles, Methods, and Tools by Alfred W. Aho, Ravi Seti, and Jeffrey D. Ullman.

+5


source







All Articles