What does this mean? Can I get some help please?

Pages: 123
This is what my Book Says:

//start
Alternative Function Syntax

The C++ 11 introduces an alternative syntax for writing the function header. Here’s an example of
the power() function that you saw earlier defi ned using the alternative syntax:

1
2
3
4
5
6
7
auto power(double x, int n)-> double // Function header
{ // Function body starts here...
double result(1.0); // Result stored here
for(int i = 1; i <= n; i++)
result *= x;
return result;
} // ...and ends here 


This will work in exactly the same way as the previous version of the function. The return type of the function appears following the -> in the header. This is referred to as a trailing return type. The auto keyword at the beginning indicates to the compiler that the return type is determined later.

So why was it necessary to introduce the alternative syntax? Isn’t the old syntax good enough? The answer is no. In the next chapter you’ll learn about function templates, where situations can arise when you need to allow for the return type from a function to vary depending on the result of executing the body of the function. You can specify that with the old syntax. The alternative
function syntax does allow you to do that, as you’ll see in Chapter 6.
//end

It clearly says that the old syntax allows you to do that so why do you need the new alternative syntax o.O. Is it just me or is this a mistake/typo?

PDF available here: http://it-ebooks.info/read/975/ Page 236, it may take a couple minutes to load. I even checked it on google books and the same "typo" is there although I cant seem to let google let me get on the same page or preview it till there again for some reason.

Thank You and I would appreciate some help please.
Last edited on
So the ->double is necessary or no?
What the hell is the point of having an automatic return type if you have to specify after the ->?
Apparently you need to use decyltype or something as the type. Idk thats why i came here for some help and if or not its a typo. The book teaches the rest just wondering if it is a typo.
@Major Tom: The "auto" in this case means "I'll define it later".


Anmol444 wrote:
It clearly says that the old syntax allows you to do that so why do you need the new alternative syntax o.O. Is it just me or is this a mistake/typo?
http://www.cplusplus.com/articles/z3wTURfi/
Some cases require it, others just use it because it is less typing.

Anmol444 wrote:
Apparently you need to use decyltype or something as the type
No, that's just from an example where trailing return type is the only way to make it work.

Anmol444 wrote:
just wondering if it is a typo
Definitely not ;)
Last edited on
Wait so its not a typo?
If you mean this piece
You can specify that with the old syntax.
it is a typo.
Last edited on
@naraku9333
1
2
3
4
auto f() -> int
{
    return 7;
}
1
2
3
4
int f()
{
    return 7;
}
It is not a typo.
ooo ok. So basically its just more convenient with the alternative syntax?

or with function templates it only works with them?
1
2
3
4
5
template<typename T>
T add(T a, T b)
{
    return a + b;
}
1
2
3
4
5
template<typename T, typename U>
/*return type*/ add(T a, U b)
{
    return a + b;
}
In the second one, how would you define the return type? You could use decltype(a + b), but a and b don't exist yet.

The above is an example of where it is required: when the type must be determined after the parameter list is known by the compiler.

Other cases are just because of scope making for less typing.

Normally, you would still use the old method - the new method just allows for some things that are otherwise very difficult or annoying to do with the old method.
Last edited on
Templates and stuff is all beyond my knowledge but once I do get to it i will check this thread again and if I have any difficulty may I pm you?
@LB How is it (the text I quoted) not a typo in the context?
So why was it necessary to introduce the alternative syntax? Isn’t the old syntax good enough? The answer is no. In the next chapter you’ll learn about function templates, where situations can arise when you need to allow for the return type from a function to vary depending on the result of executing the body of the function. You can specify that with the old syntax. The alternative
function syntax does allow you to do that, as you’ll see in Chapter 6.


You're own example suggests it's a typo.
Getting help by private message is very rude - it's basically like saying "I'm so important I can get help in private and no one else with the same problems can see the help I get". It's why forums exist in the first place, otherwise we'd all use email.
Woops forgot to include pdf here you go http://it-ebooks.info/read/975/
ok thankyou just wanted to ask xD
@naraku9333
1
2
3
4
5
template<typename T, typename U>
decltype(*(T*)(0) + *(U*)(0)) add(T a, U b)
{
    return a + b;
}
It is very ugly, but it is allowed and it works and it uses the old syntax. Not a typo.
@L B how would that look with trailing return type?
1
2
3
4
5
template<typename T, typename U>
auto add(T a, U b) -> decltype(a + b)
{
    return a + b;
}



Seriously, this is all from the article I linked to in my first post here. is no one reading it? It's short.
And that automatically determines the return type?

Well I understand now thank you.
@Anmol444 no, it does not automatically determine the return type. As I told Major Tom, the use of "auto" here just means "I'm using trailing return type syntax not the old syntax kthxbye"
Pages: 123