Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

c++11 - What is the meaning of auto when using C++ trailing return type?

Instead of usual

void foo (void ) {
    cout << "Meaning of life: " << 42 << endl;
}

C++11 allows is an alternative, using the Trailing Return

auto bar (void) -> void {
    cout << "More meaning: " << 43 << endl;
}

In the latter - what is auto designed to represent?

Another example, consider function

auto func (int i) -> int (*)[10] {

}

Same question, what is the meaning of auto in this example?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In general, the new keyword auto in C++11 indicates that the type of the expression (in this case the return type of a function) should be inferred from the result of the expression, in this case, what occurs after the ->.

Without it, the function would have no type (thus not being a function), and the compiler would end up confused.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...