Trailing Return Types

Im struggling to understand Trailing return types. Ive bee searching for simple example to understand it but cant find anything


Basically..If i have a function declaration like this
func(int i) -> int (*)[10];

How am i supposed to define it? Is it like this?
1
2
3
4
auto func(int i) -> int(*)[10]{

   return ?// what is it that im supposed to return?
}


but the problem is I dont know what I am supposed to return? Returning i is invalid.


Again as from my previous questions this is all about the book that i am reading to learn c++ and sometimes doesnt give much information about a certain like this trailing return type that leaves me scratching my head.


This btw about a function that return an array which is illegal in c++ but can attain the result in different way for example

1
2
3
typedef int arrT[10]; 
using arrtT = int[10]; 
arrT* func(int i);


again the author of that book explains what should happen but didnt use it to a usefull example.




So can someone guide me here? Thanks
Last edited on
know what I am supposed to return
pointer to array of 10 ints: http://puu.sh/9iHNv/fe080659a3.png
You can manage to return it by returning pointer to static or global array:
1
2
3
4
5
auto func(int i) -> int(*)[10]
{
   static int x[10];
   return &x;
}


Your second example declares function returning the same thing, but uses type aliasing to not confuse C++ parser.
Last edited on
@MiiNiPaa

Isnt it illegal to return a reference of local variable of a function?
It would be undefined.

It is not local. It is static. It will be created when function first executed and will exist until program finishes. It will not be recreated between function calls and it is a possibility for function to save its state between calls.
Last edited on
I see.. Now i get it.

Thank you very much MiiNiPaa. Helps me alot.
Topic archived. No new replies allowed.