" after a function declaration? I just met the following C ++ functio...">

Using & # 8594; in function description

Possible duplicate:
What is a "->" after a function declaration?

I just met the following C ++ function examples using the new auto keyword, and I was hoping someone could help me understand what the syntax means.

 template <class T, class U> auto add(T t, U u) -> decltype(t + u); auto f = [](int a, int b) -> int { return a*b; }; 

In particular, I am confused about the user -> in the function signature, and I expect them to be written in the format

 template <class T, class U> auto add(T t, U u) { decltype(t + u); } auto f = [](int a, int b){ return a*b; }; 

What does the -> operator do, and where can I learn more about this syntax?

+6
source share
6 answers

What does the operator -> there?

This type of return is refundable. Instead:

 int f(); 

you can equivalently write:

 auto f() -> int; 

If the return type depends on the types of function parameters, you need to use this form; parameters are not available until they are declared:

 decltype(t+u) add(T t, U u); // Error: uses `t` and `u` before they're declared auto add(T t, U u) -> decltype(t + u); // OK 

In addition, if you want to specify the return type of lambda, you must use this form; although, as you point out, in many cases (including this one) you do not need to indicate this at all.

+13
source

[dcl.spec.auto] / 2 explains how to write a function declaration with an automatic return type:

An automatic type specifier can be displayed using a function declarator with a return type of return type (8.3.5) in any context in which such an identifier is valid.

And then, in [dcl.fct] / 12, there is a note:

Typedefs and trailing-return-types are sometimes convenient when the return type of a function is complex. For example, the fpif function above could be declared

 typedef int IFUNC(int); IFUNC* fpif(int); 

or

 auto fpif(int)->int(*)(int) 

The trailing-return type is most useful for a type that will be more difficult to specify before the declarator identifier:

 template <class T, class U> auto add(T t, U u) -> decltype(t + u); 

but not

 template <class T, class U> decltype((*(T*)0) + (*(U*)0)) add(T t, U u); 
+3
source

This syntax (called trailing-return-type) is a workaround for using the expression as the return type as follows:

 template <class T, class U> decltype(t + u) add(T t, U u) { ... } 

... will not be correct in C ++.

I must say that this problem is explained quite well (I suppose) in the Wiki .

+2
source

-> is the return type of return.

C ++ 11 proposed an alternative syntax for declaring a function. The auto keyword takes the place of the usual type of the return function, and the actual type of the return value begins after -> .

For instance,

 auto f (int a, int b) -> int; 

equivalently

 int f(int a, int b); 

This function is most useful for template functions where the return type must be inferred from the template parameters.

For instance,

 template <class T, class U> auto add(T t, U u) -> decltype(t + u); 

the type of the return value will be the type of the expression (t+u) .

+1
source

What does the operator → where I can learn more about this syntax?

There is a very good explanation about this at cprogramming .

The essence of using → (in your case) or auto, decltype is convenience and allows you to focus more on programming logic.

Return Type of the return value (->) allows you to include information about the return type in the function declaration itself.

In the case of your alternative example:

 auto add(T t, U u) { decltype(t + u); } 

If the function is quite complex, then it would be rather difficult (not obvious) for the reader of your program to figure out what the expected return type is.

+1
source

The → operator in function declarations indicates the return type for functions that return "auto". It is defined in chapter 8 of the C ++ 11 standard.

0
source

All Articles