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
286 views
in Technique[技术] by (71.8m points)

c++ - return (a) vs. return a

I have seen both in the C and C++ code I have been looking at.

What is the difference?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

No difference at all.

The official syntax is return something; or return; and of course it is a keyword, not a function.

For this reason you should not read it as return( a ); but as return (a); I think the difference is subtle but clear, parentheses will not apply to return but to a.

((((a)))) is the same as (a) that is the same as a.

You can also write something like...

int x = (((100)));

You can also write something like...

printf("%d
", (z));

As someone said in the comments, there is now, with C++11 (2011 version of the C++ language) the new operator decltype. This operator introduces a new example where (a) is different from a, this is quite esoteric and a little out of topic but I add this example just for the purpose of completeness.

    int x = 10;
    decltype(x) y = x;   // this means int y = x;
    decltype((x)) z = x; // this means int& z = x;
    y = 20;
    z = 30;
    std::cout << x << " " << y << " " << z << std::endl;
    // this will print out "30 20 30"

Students will not be interested in this, as I said, too esoteric, and it will work only with compilers that supports at least part of the C++11 standard (like GCC 4.5+ and Visual Studio 2010).

This goes in contrast also with the use of typeid keyword:

int a;
std::cout << typeid(a).name() << std::endl; // will print "int"
std::cout << typeid((a)).name() << std::endl; // will print "int" !!!!

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

1.4m articles

1.4m replys

5 comments

56.8k users

...