syntax - what is -> in c++ in a function declaration -


this question has answer here:

in wikipedia article on decltype http://en.wikipedia.org/wiki/decltype came across example:

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

though understood motive behind function, didnot understand syntax uses , -> in declaration. -> , how interpreted?

edit 1

based on above: wrong here?

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

the error is:

error: expected type-specifier before ‘decltype’ error: expected initializer before ‘decltype 

answer edit 1:

oops! forgot use -std=c++11 compiler option in g++.

edit 2

based on below answer. have related question: @ declaration below:

template <typename t1, typename t2> decltype(*(t1 *) nullptr + *(t2 *) nullptr) sum2(t1 v1, t2 v2); 

it using decltype without need -> in function declaration. why need ->

this uses trailing return type notation. this:

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

is equivalent this:

t f() { ... } 

the advantage trailing return type notation can express type of function based on expressions involve arguments, not possible classical notation. instance, illegal:

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

concerning edit:

based on above: wrong here?

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

nothing.

concerning second edit:

[...] using decltype without need -> in function declaration. why need ->

in case don't need it. however, notation use trailing return type clearer, 1 may prefer make code easier understand.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -