most-vexing-parse

Most vexing parse

橙三吉。 提交于 2019-11-25 22:01:45
问题 I got the code from here. class Timer { public: Timer(); }; class TimeKeeper { public: TimeKeeper(const Timer& t); int get_time() { return 1; } }; int main() { TimeKeeper time_keeper(Timer()); return time_keeper.get_time(); } From the looks of it, it should get compile error due to the line: TimeKeeper time_keeper(Timer()); But it only happens if return time_keeper.get_time(); is present. Why would this line even matter, the compiler would spot ambiguity on time_keeper(Timer() ) construction.

Default constructor with empty brackets

家住魔仙堡 提交于 2019-11-25 21:36:28
问题 Is there any good reason that an empty set of round brackets (parentheses) isn\'t valid for calling the default constructor in C++? MyObject object; // ok - default ctor MyObject object(blah); // ok MyObject object(); // error I seem to type \"()\" automatically everytime. Is there a good reason this isn\'t allowed? 回答1: Most vexing parse This is related to what is known as "C++'s most vexing parse". Basically, anything that can be interpreted by the compiler as a function declaration will be