另一个容器内的c++容器
#include <bits/stdc++.h>
template <typename T>
struct Row {
Row() { puts("Row default"); }
Row(const Row& other) { puts("Row copy"); }
Row(Row&& other) { puts("Row move"); }
Row(T x) { puts("Row int"); }
};
int main() {
Row<Row<int>> x (10); // this works
Row<Row<int>> x2 = 10; // error
}
为什么代码不起作用?我知道第一个执行直接初始化,第二个是复制初始化,但是当涉及到容器内的容器时,我对我来说并不容易。
编译器:clang c++20
错误信息:
没有从“int”到“Row<Row>”的可行转换
行<行> x2 = 10
回答
问题是复制初始化不如直接初始化宽松:
> 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.
由于初始化 fromint需要 2 次转换(从inttoRow<int>和 from Row<int>to Row<Row<int>>),直接初始化允许,但复制初始化不允许。您也可以在文档示例中看到它:
struct S { S(std::string) {} }; // implicitly convertible from std::string
S s("abc"); // OK: conversion from const char[4] to std::string
S s = "abc"; // Error: no conversion from const char[4] to S
S s = "abc"s; // OK: conversion from std::string to S