What Is "->" After Function Declaration

What is - after function declaration?

It's the new function declaration syntax from C++11, and it's called the "trailing return type". At the end of a function declaration, -> means that the following is the return type of the function. It can only be used when the auto keyword is used instead of an actual return type where you would normally expect it.

For instance, these two declarations are compatible:

int foo();
auto foo() -> int;

Depending on your tastes, you may find it prettier than the old declaration syntax, especially when the return type is extremely long/complex:

task<typename details::_TaskTypeFromParam<_Ty>::_Type> create_task(_Ty _Param);
auto create_task(_Ty _Param) -> task<typename details::_TaskTypeFromParam<_Ty>::_Type>;

But sometimes it can be necessary with templates, when the return type of the function could vary with the arguments.

Say you want a templated function to add variables:

template<typename T>
T add(const T& x, const T& y)
{
return x + y;
}

That's great, but you'll only be able to add variables of the same type. Suppose you would like to be able to add variables of any type (like add((int)1, (double)2)).

template<typename T, typename U>
??? add(const T& x, const U& y)
{
return x + y;
}

EDIT: note that in C++14 and onwards, it's legal to write auto add(const T& x, const U& y), without a trailing return type, for function definitions (in other words, when you define the body of your function).


The problem is that you can't tell in advance what the result type of x + y will be. As templates stand, they could even be non-integral types. (Wouldn't you like to be able to do add(std::string("x"), "y")?)

Decltype, along with the new function declaration syntax, lets you solve this problem.

template<typename T, typename U>
auto add(const T& x, const U& y) -> decltype(x + y)
{
return x + y;
}

Decltype "returns" the type of an expression. Since you need x and y to have been declared for decltype(x + y) to work, you need the new syntax.

Function declaration before and after main()

There are two valid signatures for the main() function:

int main( void )
int main( int argc, char *argv[] )

Notice that both valid signatures have a return type of int. Any other return type, such as void is not valid and causes the compiler to output a warning message.

When the code calls a function, the compiler needs to know the signature of that called function. There are two ways to tell the compiler what the signature of the called function is:

  1. have the whole function listed before where it is called
  2. have a prototype (aka forward reference) of the function signature before where the function is called. In a prototype the compiler only needs the returned type and the types of the parameters. However, it is a good programming practice to have the names of the parameters listed in the prototype as a courtesy to the humans reading the code.

What does a line after function declaration but outside function itself

This is the original (read: ancient) style of declaring argument parameters in K&R C. In ANSI C standards, the form you're likely familiar with has been the standard.

See also: What are the major differences between ANSI C and K&R C?

(this) after function declaration

This whole structure is a means of saving the current value of this so that a function call later on can use it.

That could all be done also with .bind() like this which (if you understand what .bind() does might be easier to follow):

function myFunc(_this) {
// do something
}

var a = myFunc.bind(null, this);

Here are the various steps in what happens in the code you've shown:

this will have a value from the surrounding context when this code is originally executed (which you don't show). It is being passed into a self-executing function as an argument often referred to as an IIFE (immediately invoked function expression) which is just a function call that happens immediately inline as the code is initially run.

Within that function it is given an argument name of _this.

When that function executes, it returns another function. The body of that inner function also has access to _this.

When that inner function is returned, it is assigned to the variable a.

The upshot of all this is that one can call a() and the internals of that function, when it executes will be able to access _this which contains the value of the original this.

So, it's essentially a means of creating a function that when executed will have access to the original value of this even though the context will have changed when a() is later called. So, its essentially saving the value of this for a specific function to use later.

More detail would require more context about what is going on inside that internal function, what the this value was in the original context and how a() is used later.


This is one particular use of an IIFE. They have many other uses.

Reference sign after method declaration c++

Yes, this is a ref-qualified member function. The lvalue-reference notation on the right makes this function callable only on lvalues of myclass, that is:

myclass c;
c.getInteger(); // OK
myclass{}.getInteger(); // Compile-time error

Colon after method declaration?

Yes it's new syntax introduced in PHP 7 to declare the method returns an array.

http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration



Related Topics



Leave a reply



Submit