为什么这不对转换构造函数进行隐式转换?
Why Doesn't This Do an Implicit Cast to the Converting Constructor?
所以我有这个代码:
struct Foo {
Foo() { cout << "default\n"; }
Foo(const long long) { cout << "implicit\n"; }
};
struct Bar {
Bar(const short param) : param(param) {}
operator long long() const { return static_cast<long long>(param); }
const short param;
};
我原以为 Foo foo = Bar(13)
会使用我的隐式转换,然后使用转换构造函数。 But it errors:
error: conversion from Bar
to non-scalar type Foo
requested
虽然这工作正常:Foo foo(Bar(13))
。为什么我的隐式转换用于显式转换构造,而不用于隐式转换构造?
我从 https://en.cppreference.com/w/cpp/language/copy_initialization 得到的规则说:
The result of the conversion, which is a prvalue expression if a converting constructor was used, is then used to direct-initialize the object
首先从Bar
到long long
的隐式转换,从long long
到Foo
的隐式转换都是用户自定义转换。
Foo foo = Bar(13);
执行 copy initialization, the compiler will try to convert Bar
to Foo
implicitly. Two implicit conversions are required, i.e. converting Bar
to long long
and then converting long long
to Foo
. But only one user-defined conversion is allowed in one implicit conversion 序列。
Implicit conversion sequence consists of the following, in this order:
1) zero or one standard conversion sequence;
2) zero or one user-defined conversion;
3) zero or one standard conversion sequence.
A user-defined conversion consists of zero or one non-explicit single-argument constructor or non-explicit conversion function call
Foo foo(Bar(13));
执行 direct initialization。将检查 Foo
的构造函数,并通过重载决策选择最佳匹配。只需要一个隐式的用户定义转换(从 Bar
到 long long
);之后调用Foo::Foo(long long)
直接构造foo
。
当你使用复制初始化然后根据documentation
In addition, the implicit conversion in copy-initialization must produce T directly from the initializer, while, e.g. direct-initialization expects an implicit conversion from the initializer to an argument of T's constructor.
重点是我的。事实并非如此。
所以我有这个代码:
struct Foo {
Foo() { cout << "default\n"; }
Foo(const long long) { cout << "implicit\n"; }
};
struct Bar {
Bar(const short param) : param(param) {}
operator long long() const { return static_cast<long long>(param); }
const short param;
};
我原以为 Foo foo = Bar(13)
会使用我的隐式转换,然后使用转换构造函数。 But it errors:
error: conversion from
Bar
to non-scalar typeFoo
requested
虽然这工作正常:Foo foo(Bar(13))
。为什么我的隐式转换用于显式转换构造,而不用于隐式转换构造?
我从 https://en.cppreference.com/w/cpp/language/copy_initialization 得到的规则说:
The result of the conversion, which is a prvalue expression if a converting constructor was used, is then used to direct-initialize the object
首先从Bar
到long long
的隐式转换,从long long
到Foo
的隐式转换都是用户自定义转换。
Foo foo = Bar(13);
执行 copy initialization, the compiler will try to convert Bar
to Foo
implicitly. Two implicit conversions are required, i.e. converting Bar
to long long
and then converting long long
to Foo
. But only one user-defined conversion is allowed in one implicit conversion 序列。
Implicit conversion sequence consists of the following, in this order:
1) zero or one standard conversion sequence;
2) zero or one user-defined conversion;
3) zero or one standard conversion sequence.
A user-defined conversion consists of zero or one non-explicit single-argument constructor or non-explicit conversion function call
Foo foo(Bar(13));
执行 direct initialization。将检查 Foo
的构造函数,并通过重载决策选择最佳匹配。只需要一个隐式的用户定义转换(从 Bar
到 long long
);之后调用Foo::Foo(long long)
直接构造foo
。
当你使用复制初始化然后根据documentation
In addition, the implicit conversion in copy-initialization must produce T directly from the initializer, while, e.g. direct-initialization expects an implicit conversion from the initializer to an argument of T's constructor.
重点是我的。事实并非如此。