What & # 8594; in c ++ in function declaration

In the Wikipedia article decltype http://en.wikipedia.org/wiki/Decltype I came across this example:

 int& foo(int& i); float foo(float& f); template <class T> auto transparent_forwarder(T& t) βˆ’> decltype(foo(t)) { return foo(t); } 

Although I understood the motive of this function, I did not understand the syntax that it uses, and in particular -> in the declaration. What is β†’ and how is it interpreted?

EDIT 1

Based on the foregoing: what is wrong here?

 template <typename T1, typename T2> auto sum(T1 v1, T2 v2) -> decltype(v1 + v2) { return v1 + v2; } 

Error:

 error: expected type-specifier before 'decltype' error: expected initializer before 'decltype 

Answer to EDIT 1:

OOPS! I forgot to use the -std=c++11 compiler option in g ++.

EDIT 2

Based on the answer below. I have a related question: See the declaration below:

 template <typename T1, typename T2> decltype(*(T1 *) nullptr + *(T2 *) nullptr) sum2(T1 v1, T2 v2); 

It uses decltype without the need of a -> function declaration. So why do we need ->

+4
source share
1 answer

In this case, a return record of the return type is used. It:

 auto f() -> T { ... } 

This is equivalent to this:

 T f() { ... } 

The advantage is that with backward note recording, you can express a function type based on expressions containing arguments, which is not possible in classical notation. For example, this would be illegal:

  template <class T> decltype(foo(t)) transparent_forwarder(T& t) { // ^^^^^^^^^^^^^^^^ // Error! "t" is not in scope here... return foo(t); } 

Regarding your editing:

Based on the foregoing: what is wrong here?

 template <typename T1, typename T2> auto sum(T1 v1, T2 v2) -> decltype(v1 + v2) { return v1 + v2; } 

Nothing.

Regarding the second edit:

[...] It uses decltype without the need for β†’ to declare a function. So why do we need β†’

In this case, you do not need it. However, a notation using the return type of the return type is much clearer, so you might prefer that the code is easier to understand.

+10
source

Source: https://habr.com/ru/post/1479155/


All Articles