【发布时间】:2021-01-03 03:36:55
【问题描述】:
#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
' 的转换不可行
行
x2 = 10
【问题讨论】:
-
请创建一个minimal reproducible example - 任何拥有符合标准的编译器的人都可以编译的东西。提示:
#include <bits/stdc++.h>不包含标准标题。 -
你需要实现
operator =(...)。