cpp's most vexing parse

本文探讨了C++中一个著名的解析难题——最困扰解析问题,即当构造函数参数为临时变量时,编译器如何区分函数声明与构造函数调用之间的歧义。虽然C++09并未解决这一问题,但文章提供了几种避免该问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++ has parsing ambiguity between a function declaration and a constructor invocation when the constructor arguments are temporaries. This is known as the "most vexing parse" problem. C++09 doesn't resolve this ambiguity but there are several things you can do to avert it in the first place. Awareness is the first step.

当参数是临时变量的时候会出现。

[refer to ]

http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=439



/home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B8.hpp:1:22: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B8_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B8.hpp:1:22: note: remove parentheses to default-initialize a variable 1 | bool hang_B8_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B8.hpp:1:22: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B8.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:57, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B9.hpp:1:22: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B9_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B9.hpp:1:22: note: remove parentheses to default-initialize a variable 1 | bool hang_B9_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B9.hpp:1:22: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B9.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:58, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B10.hpp:1:23: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B10_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B10.hpp:1:23: note: remove parentheses to default-initialize a variable 1 | bool hang_B10_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B10.hpp:1:23: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B10.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:59, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B11.hpp:1:23: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B11_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B11.hpp:1:23: note: remove parentheses to default-initialize a variable 1 | bool hang_B11_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B11.hpp:1:23: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B11.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:60, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B12.hpp:1:23: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B12_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B12.hpp:1:23: note: remove parentheses to default-initialize a variable 1 | bool hang_B12_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B12.hpp:1:23: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B12.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:61, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B13.hpp:1:23: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B13_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B13.hpp:1:23: note: remove parentheses to default-initialize a variable 1 | bool hang_B13_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B13.hpp:1:23: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B13.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:62, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B14.hpp:1:23: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B14_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B14.hpp:1:23: note: remove parentheses to default-initialize a variable 1 | bool hang_B14_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B14.hpp:1:23: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B14.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:63, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B15.hpp:1:23: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B15_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B15.hpp:1:23: note: remove parentheses to default-initialize a variable 1 | bool hang_B15_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B15.hpp:1:23: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B15.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:16, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/黑匣子.hpp: In member function ‘virtual void hang_B7_zhi_xing()::leiHeiXiaZi_2_16::luo_ji()’: /home/getcrobot/文件_5_9_16.24现在/include/注册/黑匣子.hpp:75:7: error: use of local variable with automatic storage from containing function 75 | FaBuZhe.luo_ji(); | ^~~~~~~ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:15, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/发布/发布者.hpp:112:17: note: ‘hang_B7_zhi_xing()::LeiFaBuZhe_2_8 FaBuZhe’ declared here 112 | LeiFaBuZhe_2_8 FaBuZhe; | ^~~~~~~ /home/getcrobot/文件_5_9_16.24现在/main/main.cpp: In function ‘bool hang_B7_zhi_xing()’: /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:12:1: error: a function-definition is not allowed here before ‘{’ token 12 | { | ^ /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:24:1: error: a function-definition is not allowed here before ‘{’ token 24 | { | ^ /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:33:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 33 | int main() | ^~ /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:33:9: note: remove parentheses to default-initialize a variable 33 | int main() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:33:9: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:34:1: error: a function-definition is not allowed here before ‘{’ token 34 | { | ^ /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:42:2: error: expected ‘}’ at end of input 42 | } | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:55, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B7.hpp:2:1: note: to match this ‘{’ 2 | { | ^ /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:42:2: warning: control reaches end of non-void function [-Wreturn-type] 42 | } | ^
最新发布
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值