Vin*_*ent 0 c++ arithmetic-expressions void c++11
我知道以下,如果可能的话,将是一个绝对不好的做法,但我想知道这是否可行.
问题如下:是否有可能在C++中(并且在某种程度上编译器不会抛出任何警告),使用返回void的函数执行无用的算术运算.
std::vector<int> v;
int i = 42 + v.resize(42);
/* How to transform the last line to execute resize and to have i = 42 */
Run Code Online (Sandbox Code Playgroud)
我知道这是愚蠢的,但这不是问题......
我不确定它有多大意义,但你可以在这里使用逗号运算符:
int i = (v.resize(42), 42);
Run Code Online (Sandbox Code Playgroud)
您可以使用逗号运算符:
int i = (v.resize(42), 42);
Run Code Online (Sandbox Code Playgroud)
并使用GCC,您可以使用其语句表达式扩展:
int i = ({v.resize(42); 42;})
Run Code Online (Sandbox Code Playgroud)
int i = ([&v]() {v.resize(42); return 42;}());
Run Code Online (Sandbox Code Playgroud)