最煩人的解析我從這里.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();}從它的外觀來看,它應該會得到編譯錯誤,原因是行:TimeKeeper time_keeper(Timer());但只有當return time_keeper.get_time();是存在的。為什么這一行甚至重要,編譯器會發現歧義time_keeper(Timer() )建筑。
2 回答
心有法竹
TA貢獻1866條經驗 獲得超5個贊
TimeKeeper time_keeper(Timer());get_time()
int main() {
// time_keeper gets interpreted as a function declaration with a function argument.
// This is definitely *not* what we expect, but from the compiler POV it's okay.
TimeKeeper time_keeper(Timer (*unnamed_fn_arg)());
// Compiler complains: time_keeper is function, how on earth do you expect me to call
// one of its members? It doesn't have member functions!
return time_keeper.get_time();}
搖曳的薔薇
TA貢獻1793條經驗 獲得超6個贊
雖然我知道標準在第13.1/3節中說,在這種情況下,定時器函數類型會被調整成為函數類型的指針,但是為什么有人希望從一開始就對其進行調整呢?在我看來,第13.1/3節造成了整個“最令人煩惱的解析”問題?
- 2 回答
- 0 關注
- 455 瀏覽
添加回答
舉報
0/150
提交
取消
