Sto*_*row 15 c++ constructor std stdstring most-vexing-parse
我遇到了一个我甚至不知道如何描述的编译错误!这完全让我感到困惑.
情况:
代码尝试使用用char*初始化的rvalue std :: string在堆栈上创建一个对象.
代码:
#include <iostream>
#include <string>
class Foo
{
public:
Foo(double d)
: mD(d)
{
}
Foo(const std::string& str)
{
try
{
mD = std::stod(str);
}
catch (...)
{
throw;
}
}
Foo(const Foo& other)
: mD(other.mD)
{
}
virtual ~Foo() {}
protected:
double mD;
};
class Bar
{
public:
Bar(const Foo& a, const Foo& b)
: mA(a)
, mB(b)
{
}
virtual ~Bar() {}
protected:
Foo mA;
Foo mB;
};
int main(int argc, char* argv[])
{
if (argc < 3) { return 0; }
Foo a(std::string(argv[1]));
Foo b(std::string(argv[2]));
Bar wtf(a, b);
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
>g++ -std=c++11 wtf.cpp
wtf.cpp: In function ‘int main(int, char**)’:
wtf.cpp:58:17: error: no matching function for call to ‘Bar::Bar(Foo (&)(std::string*), Foo (&)(std::string*))’
Bar wtf(a, b);
^
wtf.cpp:38:9: note: candidate: Bar::Bar(const Foo&, const Foo&)
Bar(const Foo& a, const Foo& b)
^
wtf.cpp:38:9: note: no known conversion for argument 1 from ‘Foo(std::string*) {aka Foo(std::basic_string<char>*)}’ to ‘const Foo&’
wtf.cpp:35:7: note: candidate: Bar::Bar(const Bar&)
class Bar
^
wtf.cpp:35:7: note: candidate expects 1 argument, 2 provided
>
Run Code Online (Sandbox Code Playgroud)
你不会相信/一个变通方法是什么(或者至少我没有).如果我在我的rvalue std :: string上调用substr(0),则编译器会被平息.但我不明白为什么这会有所作为.毕竟...
std::string(argv[1]).substr(0)
Run Code Online (Sandbox Code Playgroud)
......本身仍然是一个价值.我不明白为什么它与编译器的观点不同.
即对main(...)的以下更改允许编译成功:
int main(int argc, char* argv[])
{
if (argc < 3) { return 0; }
Foo a(std::string(argv[1]).substr(0));
Foo b(std::string(argv[2]).substr(0));
Bar wtf(a, b);
}
Run Code Online (Sandbox Code Playgroud)
几个额外的数据点:
我死去知道这是什么问题.左边的场地让人感觉如此.
谢谢你的帮助.
Bri*_*ian 28
这是最令人烦恼的解析的一个不太常见的例子.声明
Foo a(std::string(argv[1]));
Run Code Online (Sandbox Code Playgroud)
不是Foo用字符串参数调用构造函数; 相反,它声明a是一个函数,取一个数组1 std::string(调整到指针std::string)并返回Foo.这就是为什么错误消息提一个Foo (&)(std::string*)类型:这是编译器认为该类型a和b是.((&)消息中的含义仅表示它是左值.)
添加.substr(0)消除歧义的歧义,以便它不能被解析为函数声明.
Brace初始化是解决问题的更优雅的方法:
Foo a{std::string(argv[1])};
Run Code Online (Sandbox Code Playgroud)
Som*_*ude 10
如果仔细观察错误消息,您将看到正在尝试使用两个函数构造Bar对象.这里发生的事情令人烦恼,令人烦恼,它被正式命名为最令人烦恼的解析.wtf
发生的事情是你宣布a和b作为功能.将指针std::string作为参数并返回Foo对象的函数.
如果你有一个支持C++ 11的编译器,你可以在构造Foo对象时使用大括号而不是括号:
Foo a{std::string(argv[1])};
Foo b{std::string(argv[2])};
Run Code Online (Sandbox Code Playgroud)
如果你有一个较旧的编译器,你可以使用复制初始化:
Foo a = Foo(std::string(argv[1]));
Foo b = Foo(std::string(argv[2]));
Run Code Online (Sandbox Code Playgroud)
如果没有std::string为构造函数参数显式创建对象,它应该也可以正常工作,让编译器处理:
Foo a(argv[1]);
Foo b(argv[2]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1239 次 |
| 最近记录: |