为什么我不能在传递给参数时隐式构造一个给定合适的构造函数的对象? [英] Why can't I implicitly construct an object given a suitable constructor when passing to an argument?
问题描述
在下面的例子中,为什么我不能简单地传递 string
到 printFoo()
?
#include< string>
#include< iostream>
using namespace std;
class Foo {
public:
Foo(const Foo& foo):str(foo.str){}
Foo(string str):str str){}
string str;
};
void printFoo(Foo foo){
cout<< foo.str< endl;
}
int main(){
Foo foo(qux);
printFoo(foo); // OK
printFoo(qix); //错误:没有匹配函数调用'printFoo'
return 0;
}
无论出于什么原因,我在我脑海中自动确定了构造函数
为什么我不能这样做,但我可以传递 char [n] $例如?
std :: string
/ div> 会涉及两个隐式转换:
- 到
std :: string
- 至
Foo
C ++最多只能有一个:
4次标准转换(N3337)
标准转换是内含意义的隐含转换。
第4条列举了这些转换的完整集合。标准的
转换序列是
中按照顺序的标准转换序列:
- 从下面的集合中进行零或一次转换:
左值到值的转换,数组到指针的转换和
函数到指针的转换。
- 从
开始的零或一次转换:集合促销,浮点促销,积分
转换,浮点转换,浮点积分
转换,指针转换,指向成员转换的指针和
布尔转换。
- 零个或一个资格转换。
)
1类对象的类型转换可以由构造函数指定,
可以由转换函数指定。这些转换称为用户定义的
转换,用于隐式类型转换(第4章),
初始化(8.5)和显式类型转换(5.4,5.2.9)。
2用户定义的转换仅在它们是无歧义的
(10.2,12.3.2)时应用。转换遵守访问控制规则(第11条)。
访问控制在模糊解析(3.4)之后应用。
[...]
4 最多一个用户定义 b $ b 转换(构造函数或转换函数)隐式应用于
到单个值。
(强调我的)
In the below example, why can't I simply pass a string
to the printFoo()
?
#include <string>
#include <iostream>
using namespace std;
class Foo {
public:
Foo(const Foo &foo) : str(foo.str) {}
Foo(string str) : str(str) {}
string str;
};
void printFoo(Foo foo) {
cout << foo.str << endl;
}
int main() {
Foo foo("qux");
printFoo(foo); // OK
printFoo("qix"); // error: no matching function for call to 'printFoo'
return 0;
}
For whatever reason, I had in my head that a constructor would automatically be determined and used in order to construct an object.
Why can't I do this, but I can pass a char[n]
constant to an argument accepting a std::string
, for example?
There would be two implicit conversions involved:
- to
std::string
- to
Foo
C++ does at most one:
From 4 Standard conversions (N3337)
Standard conversions are implicit conversions with built-in meaning. Clause 4 enumerates the full set of such conversions. A standard conversion sequence is a sequence of standard conversions in the following order:
— Zero or one conversion from the following set: lvalue-to-rvalue conversion, array-to-pointer conversion, and function-to-pointer conversion.
— Zero or one conversion from the following set: integral promotions, floating point promotion, integral conversions, floating point conversions, floating-integral conversions, pointer conversions, pointer to member conversions, and boolean conversions.
— Zero or one qualification conversion.
Also 12.3 Conversions (N3337)
1 Type conversions of class objects can be specified by constructors and by conversion functions. These conversions are called user-defined conversions and are used for implicit type conversions (Clause 4), for initialization (8.5), and for explicit type conversions (5.4, 5.2.9).
2 User-defined conversions are applied only where they are unambiguous (10.2, 12.3.2). Conversions obey the access control rules (Clause 11). Access control is applied after ambiguity resolution (3.4).
[...]
4 At most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value.
(Emphasis mine)
这篇关于为什么我不能在传递给参数时隐式构造一个给定合适的构造函数的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!