没有用于初始化“字符串"(又称为"basic_string< char>")的匹配构造函数 [英] no matching constructor for initialization of 'string' (aka 'basic_string<char>')
问题描述
这是代码:
#include< iostream>#include< string>使用命名空间std;Foo类{上市:运算符string()const {return n;}字符串n {"foo"};};int main(int argc,char ** argv){字符串s {Foo {}};cout<<s<<恩德尔返回0;}
此代码使用gcc 4.8.3进行编译,但不使用clang 3.5进行编译,有人可以告诉我这是怎么回事吗?
我遇到了这样的错误:
main.cpp:45:12:错误:没有匹配的构造函数来初始化'string'(aka'basic_string< char>')字符串s {Foo {}};^ ~~~~~~~~
clang --version:
clang版本3.5.0(标签/RELEASE_350/final 216961)目标:x86_64-suse-linux螺纹型号:posix
谢谢
我相信这是一个clang错误.根据[dcl.init.list]中列表初始化的规则:
对象或类型为
T
的引用的列表初始化定义如下:
- 如果
派生的类T
是类类型,并且初始化列表具有单个类型为 cvU
的元素,其中U
是T
或从T
,[...]- 否则,如果
T
是字符数组,并且[...]- 否则,如果
T
是一个聚合,则[...]- 否则,如果初始化列表中没有元素[...]
- 否则,如果
T
是std :: initializer_list< E>
的特殊化,则[...]- 否则,如果
T
是类类型,则考虑构造函数.列举适用的构造函数并通过过载分辨率(13.3、13.3.1.7)来选择最佳的.如果转换范围缩小(请参见以下)转换为任何参数,则程序格式不正确.- [...]
T
是类类型,因此我们考虑 basic_string
构造函数.该列表中的#7(复制构造函数)是适用的可行构造函数,因此应选择它.届时,这些表达式应等效:
struct Foo {运算符std :: string()const {返回"hello";}};std :: string s {Foo {}};//错误std :: string s(Foo {});//好的std :: string s = Foo {};//好的
但是由于某种原因,在列表初始化的情况下,clang抱怨有:
从
Foo
到const std :: __ cxx11 :: basic_string< char>的未知转换&
作为第一个参数
尽管有,所以我将其归档为 LLVM错误23658 ./p>
Here is the code:
#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;
}
This code compiles using gcc 4.8.3, but it does not compile using clang 3.5, can someone tell me what's wrong with it?
I got an error like this:
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
Thanks
I believe this is a clang bug. According to the rules for list-initialization in [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 cvU
, whereU
isT
or a class derived fromT
, [...]- 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 ofstd::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
is a class type, so we consider the basic_string
constructors. #7 in that list (the copy constructor) is an applicable, viable constructor and so it should be chosen. At that point, these expressions should be equivalent:
struct Foo {
operator std::string() const { return "hello"; }
};
std::string s{Foo{}}; // error
std::string s(Foo{}); // OK
std::string s = Foo{}; // OK
For some reason though, in the list-initialization case, clang complains that there is:
no known conversion from
Foo
toconst std::__cxx11::basic_string<char> &
for 1st argument
There is though, so I filed this as LLVM Bug 23658.
这篇关于没有用于初始化“字符串"(又称为"basic_string< char>")的匹配构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!