gpt4 book ai didi

c++ - 没有用于初始化 'string'(又名 'basic_string')的匹配构造函数

转载 作者:搜寻专家 更新时间:2023-10-31 00:12:21 24 4
gpt4 key购买 nike

代码如下:

#include <iostream>
#include <string>

using namespace std;

class Foo {
public:
operator string() const { return n; }
string n {"foo"};
};

int main (int argc, char** argv) {

string s {Foo{}};
cout << s << endl;

return 0;
}

此代码使用 gcc 4.8.3 编译,但不使用 clang 3.5 编译,有人可以告诉我它有什么问题吗?

我遇到这样的错误:

main.cpp:45:12: error: no matching constructor for initialization of 'string' (aka 'basic_string<char>')
string s {Foo{}};
^ ~~~~~~~

clang --version:

clang version 3.5.0 (tags/RELEASE_350/final 216961)
Target: x86_64-suse-linux
Thread model: posix

谢谢

最佳答案

我相信这是一个 clang 错误。根据 [dcl.init.list] 中的列表初始化规则:

List-initialization of an object or reference of type T is defined as follows:

  • If T is a class type and the initializer list has a single element of type cv U, where U is T or a class derived from T, [...]
  • Otherwise, if T is a character array and [...]
  • Otherwise, if T is an aggregate, [...]
  • Otherwise, if the initializer list has no elements [...]
  • Otherwise, if T is a specialization of std::initializer_list<E>, [...]
  • Otherwise, if T is a class type, constructors are considered. The applicable constructors are enumerated and the best one is chosen through overload resolution (13.3, 13.3.1.7). If a narrowing conversion (see below) is required to convert any of the arguments, the program is ill-formed.
  • [...]

T是类类型,所以我们考虑 basic_string constructors .该列表中的#7(复制构造函数)是一个适用的、可行的构造函数,因此应该选择它。此时,这些表达式应该是等价的:

struct Foo {
operator std::string() const { return "hello"; }
};

std::string s{Foo{}}; // error
std::string s(Foo{}); // OK
std::string s = Foo{}; // OK

但是出于某种原因,在列表初始化的情况下,clang 提示有:

no known conversion from Foo to const std::__cxx11::basic_string<char> & for 1st argument

虽然有,所以我将其归档为 LLVM Bug 23658 .

关于c++ - 没有用于初始化 'string'(又名 'basic_string<char>')的匹配构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30461583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com