C++ 编译错误用右值 std::string 构造对象

标签 c++ constructor std stdstring most-vexing-parse

我遇到了一个我什至不知道如何描述的编译错误!这完全让我感到困惑。

情况:

代码尝试在堆栈上创建一个带有右值 std::string 的对象,该右值用 char* 初始化。

代码:

#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);
}

编译器错误:

>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
>

你也不会相信/a 解决方法是什么(或者至少不相信)。如果我在我的右值 std::string 上调用 substr(0),编译器就会被安抚。但我不明白为什么这会有所作为。毕竟……

std::string(argv[1]).substr(0)

...本身仍然是一个右值。我不明白为什么它与编译器的观点不同。

即对 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);
}

几个额外的数据点:

  • 在没有 C++11 的情况下编译没有区别(我只包含它是为了访问 std::stod(...),这不是重点)。
  • g++ (GCC) 5.4.0。
  • 编译环境是cygwin。
  • 我已尝试修改 Bar 的构造函数以采用 std::string(而不是 std::string&)——它不会影响编译错误。

渴望知道这个问题是什么。这感觉太离谱了。

感谢您的帮助。

最佳答案

这是 most vexing parse 的一个不太常见的示例.声明

Foo a(std::string(argv[1]));

没有用字符串参数调用 Foo 的构造函数;相反,它声明 a 是一个函数,采用 1 个 std::string 数组(调整为指向 std::string 的指针) 并返回 Foo。这就是错误消息提到 Foo (&)(std::string*) 类型的原因:这是编译器认为 ab 的类型> 是。 (消息中的 (&) 仅表示它是一个左值。)

添加 .substr(0) 可以消除声明的歧义,使其不能被解析为函数声明。

大括号初始化是一种更优雅的解决问题的方法:

Foo a{std::string(argv[1])};

关于C++ 编译错误用右值 std::string 构造对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38989211/

相关文章:

c++ - 在 JNI 和 C++ 之间发送参数的最佳方式

c++ - 断言模板参数是迭代器/指针

c++ - 无法在 RHEL 7 上使用 CUDA 构建 OpenCV

javascript - Angular 4 : When and why is @Inject is used in constructor?

c++ - 尝试实现 std::tie 和 std::tuple 的小版本

c++ - Vim 转到 std 库方法/函数定义

java - 通过 C++/Java 进行音频编辑

c++ - 初始化结构的私有(private)成员

c++ - 构造函数初始值设定项列表未调用复制构造函数

c++ - std::for_each,具有 operator() 的对象重载而不维护状态