我试图通过初始化列表将字符串文字数组传递给仅接受的函数const char**。示例代码如下:
// Example program
void foo(const char **) { }
int main() {
using argType = const char*[];
foo(argType{"a","b"});
}
Run Code Online (Sandbox Code Playgroud)
在GCC中不编译。错误是:
在函数'int main()'中:6:25:错误:获取临时数组的地址
我知道此参数是一个临时变量,将在执行此foo(...)语句后清除。但是,为什么这种情况被编译器视为错误?
现在,如果我std::move在两者之间添加:
foo(std::move(argType{"a","b"}));
Run Code Online (Sandbox Code Playgroud)
海湾合作委员会停止抱怨。为什么?
c++ ×1