了解复制初始化和隐式转换
Understanding copy-initialisation and implicit conversions
我无法理解为什么以下复制初始化无法编译:
#include <memory>
struct base{};
struct derived : base{};
struct test
{
test(std::unique_ptr<base>){}
};
int main()
{
auto pd = std::make_unique<derived>();
//test t(std::move(pd)); // this works;
test t = std::move(pd); // this doesn't
}
A unique_ptr<derived>
可以移动到 unique_ptr<base>
,那么为什么第二个语句有效而最后一个却不行?执行复制初始化时是否不考虑非显式构造函数?
gcc-8.2.0 的错误是:
conversion from 'std::remove_reference<std::unique_ptr<derived, std::default_delete<derived> >&>::type'
{aka 'std::unique_ptr<derived, std::default_delete<derived> >'} to non-scalar type 'test' requested
来自 clang-7.0.0 的是
candidate constructor not viable: no known conversion from 'unique_ptr<derived, default_delete<derived>>'
to 'unique_ptr<base, default_delete<base>>' for 1st argument
实时代码可用 here。
很确定编译器只允许考虑单个隐式转换。在第一种情况下,只需要从 std::unique_ptr<derived>&&
转换为 std::unique_ptr<base>&&
,在第二种情况下,基指针也需要转换为 test
(默认移动构造函数才能工作)。
因此,例如将派生指针转换为基数:std::unique_ptr<base> bd = std::move(pd)
,然后移动分配它也可以。
A std::unique_ptr<base>
与 std::unique_ptr<derived>
不是同一类型。当你这样做时
test t(std::move(pd));
您调用 std::unique_ptr<base>
的转换构造函数将 pd
转换为 std::unique_ptr<base>
。这很好,因为您可以进行单个用户定义的转换。
在
test t = std::move(pd);
您正在进行复制初始化,因此您需要将 pd
转换为 test
。这需要 2 个用户定义的转换,但你不能那样做。您首先必须将 pd
转换为 std::unique_ptr<base>
,然后您需要将其转换为 test
。这不是很直观,但是当你有
type name = something;
无论 something
是什么,都需要是来自源类型的单个用户定义转换。在你的情况下,这意味着你需要
test t = test{std::move(pd)};
它只使用像第一种情况那样定义的单个隐式用户。
让我们删除 std::unique_ptr
并查看一般情况。由于 std::unique_ptr<base>
与 std::unique_ptr<derived>
不是同一类型,我们基本上有
struct bar {};
struct foo
{
foo(bar) {}
};
struct test
{
test(foo){}
};
int main()
{
test t = bar{};
}
和 we get the same error 因为我们需要从 bar -> foo -> test
开始,而且有一个用户定义的转换太多了。
[dcl.init] ¶17 中描述了初始化器的语义。直接初始化与复制初始化的选择将我们带入两个不同的项目符号之一:
If the destination type is a (possibly cv-qualified) class type:
[...]
Otherwise, if the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source
type is the same class as, or a derived class of, the class of the
destination, constructors are considered. The applicable constructors
are enumerated ([over.match.ctor]), and the best one is chosen through
overload resolution. The constructor so selected is called to
initialize the object, with the initializer expression or
expression-list as its argument(s). If no constructor applies, or the
overload resolution is ambiguous, the initialization is ill-formed.
Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source
type to the destination type or (when a conversion function is used)
to a derived class thereof are enumerated as described in
[over.match.copy], and the best one is chosen through overload
resolution. If the conversion cannot be done or is ambiguous, the
initialization is ill-formed. The function selected is called with the
initializer expression as its argument; if the function is a
constructor, the call is a prvalue of the cv-unqualified version of
the destination type whose result object is initialized by the
constructor. The call is used to direct-initialize, according to the
rules above, the object that is the destination of the
copy-initialization.
在直接初始化的情况下,我们输入第一个引用的项目符号。正如那里所详述的那样,构造函数被直接考虑和枚举。因此,所需的隐式转换序列只是将 unique_ptr<derived>
转换为 unique_ptr<base>
作为构造函数参数。
在复制初始化的情况下,我们不再直接考虑构造函数,而是试图看看哪种隐式转换序列是可能的。唯一可用的是 unique_ptr<derived>
到 unique_ptr<base>
到 test
。由于隐式转换序列只能包含一个用户定义的转换,因此这是不允许的。因此初始化格式错误。
可以说使用直接初始化排序 "bypasses" 一种隐式转换。
我无法理解为什么以下复制初始化无法编译:
#include <memory>
struct base{};
struct derived : base{};
struct test
{
test(std::unique_ptr<base>){}
};
int main()
{
auto pd = std::make_unique<derived>();
//test t(std::move(pd)); // this works;
test t = std::move(pd); // this doesn't
}
A unique_ptr<derived>
可以移动到 unique_ptr<base>
,那么为什么第二个语句有效而最后一个却不行?执行复制初始化时是否不考虑非显式构造函数?
gcc-8.2.0 的错误是:
conversion from 'std::remove_reference<std::unique_ptr<derived, std::default_delete<derived> >&>::type'
{aka 'std::unique_ptr<derived, std::default_delete<derived> >'} to non-scalar type 'test' requested
来自 clang-7.0.0 的是
candidate constructor not viable: no known conversion from 'unique_ptr<derived, default_delete<derived>>'
to 'unique_ptr<base, default_delete<base>>' for 1st argument
实时代码可用 here。
很确定编译器只允许考虑单个隐式转换。在第一种情况下,只需要从 std::unique_ptr<derived>&&
转换为 std::unique_ptr<base>&&
,在第二种情况下,基指针也需要转换为 test
(默认移动构造函数才能工作)。
因此,例如将派生指针转换为基数:std::unique_ptr<base> bd = std::move(pd)
,然后移动分配它也可以。
A std::unique_ptr<base>
与 std::unique_ptr<derived>
不是同一类型。当你这样做时
test t(std::move(pd));
您调用 std::unique_ptr<base>
的转换构造函数将 pd
转换为 std::unique_ptr<base>
。这很好,因为您可以进行单个用户定义的转换。
在
test t = std::move(pd);
您正在进行复制初始化,因此您需要将 pd
转换为 test
。这需要 2 个用户定义的转换,但你不能那样做。您首先必须将 pd
转换为 std::unique_ptr<base>
,然后您需要将其转换为 test
。这不是很直观,但是当你有
type name = something;
无论 something
是什么,都需要是来自源类型的单个用户定义转换。在你的情况下,这意味着你需要
test t = test{std::move(pd)};
它只使用像第一种情况那样定义的单个隐式用户。
让我们删除 std::unique_ptr
并查看一般情况。由于 std::unique_ptr<base>
与 std::unique_ptr<derived>
不是同一类型,我们基本上有
struct bar {};
struct foo
{
foo(bar) {}
};
struct test
{
test(foo){}
};
int main()
{
test t = bar{};
}
和 we get the same error 因为我们需要从 bar -> foo -> test
开始,而且有一个用户定义的转换太多了。
[dcl.init] ¶17 中描述了初始化器的语义。直接初始化与复制初始化的选择将我们带入两个不同的项目符号之一:
If the destination type is a (possibly cv-qualified) class type:
[...]
Otherwise, if the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, constructors are considered. The applicable constructors are enumerated ([over.match.ctor]), and the best one is chosen through overload resolution. The constructor so selected is called to initialize the object, with the initializer expression or expression-list as its argument(s). If no constructor applies, or the overload resolution is ambiguous, the initialization is ill-formed.
Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in [over.match.copy], and the best one is chosen through overload resolution. If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call is a prvalue of the cv-unqualified version of the destination type whose result object is initialized by the constructor. The call is used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization.
在直接初始化的情况下,我们输入第一个引用的项目符号。正如那里所详述的那样,构造函数被直接考虑和枚举。因此,所需的隐式转换序列只是将 unique_ptr<derived>
转换为 unique_ptr<base>
作为构造函数参数。
在复制初始化的情况下,我们不再直接考虑构造函数,而是试图看看哪种隐式转换序列是可能的。唯一可用的是 unique_ptr<derived>
到 unique_ptr<base>
到 test
。由于隐式转换序列只能包含一个用户定义的转换,因此这是不允许的。因此初始化格式错误。
可以说使用直接初始化排序 "bypasses" 一种隐式转换。