无法在初始化中转换类型
Cannot convert Type in initialization
我想我错过了什么,我不知道到底是什么。让我们看一下代码片段。
template <typename T>
struct Foo {
Foo (int n, int p, string s, T t = {})
: m_n {n}, m_p {p}, m_s {s}, m_t {t}
{}
const int m_n;
const int m_p;
const string m_s;
T m_t;
};
用法如下:
Foo<int> f_int1 {1, 2, "A", 155};
Foo<int> f_int2 {1, 2, "A"};
一切都如预期的那样。但是当我想将用户定义的类型作为 Foo 的 T 参数时,会发生一些错误。考虑:
struct Boo {
int z;
int l;
};
和用法:
Foo<Boo> f_boo1 {1, 2, "A"};
Foo<Boo> f_boo2 {1, 2, "A", {1, 2}};
这两条指令给出 (gcc 4.8.1):
cannot convert ‘Boo’ to ‘int’ in initialization
我可以这样创建 Boo 对象:
Boo boo1 {};
Boo boo2 {1, 2};
那么,你能告诉我问题出在哪里吗?
可能的解决方案:
struct Boo {
Boo () : z {}, l {} {}
Boo (int p1, int p2) : z {p1}, l {p2} {}
int z;
int l;
};
以下两个说明都按预期工作:
Foo<Boo> f_boo1 {1, 2, "A"};
Foo<Boo> f_boo2 {1, 2, "A", {1, 2}};
对我来说,没关系,我看不出有什么理由不向 class 添加两个构造函数,但如果类型不是我的怎么办?我应该用构造函数编写简单的包装器吗?
谢谢,
阿图尔
那是因为您正在尝试对 Boo
执行聚合初始化。见§8.5.4/3:
List-initialization of an object or reference of type T
is defined as follows:
— If T
is an aggregate, aggregate initialization is performed (8.5.1).
您打算复制构造您的 Boo
...但实际上您正在进行聚合初始化,这导致尝试从 Boo
构造 int z
,因此错误
error: no viable conversion from 'Boo' to 'int'
请注意,您可以在没有任何模板的情况下用更少的代码重现您的问题:
Boo b;
Boo b2{b}; // error
修复很简单。只是不要使用列表初始化:
template <typename T>
struct Foo {
Foo (int n, int p, string s, T t = {})
: m_n {n}, m_p {p}, m_s {s}, m_t(t)
// ^^^^^^
{};
我想我错过了什么,我不知道到底是什么。让我们看一下代码片段。
template <typename T>
struct Foo {
Foo (int n, int p, string s, T t = {})
: m_n {n}, m_p {p}, m_s {s}, m_t {t}
{}
const int m_n;
const int m_p;
const string m_s;
T m_t;
};
用法如下:
Foo<int> f_int1 {1, 2, "A", 155};
Foo<int> f_int2 {1, 2, "A"};
一切都如预期的那样。但是当我想将用户定义的类型作为 Foo 的 T 参数时,会发生一些错误。考虑:
struct Boo {
int z;
int l;
};
和用法:
Foo<Boo> f_boo1 {1, 2, "A"};
Foo<Boo> f_boo2 {1, 2, "A", {1, 2}};
这两条指令给出 (gcc 4.8.1):
cannot convert ‘Boo’ to ‘int’ in initialization
我可以这样创建 Boo 对象:
Boo boo1 {};
Boo boo2 {1, 2};
那么,你能告诉我问题出在哪里吗?
可能的解决方案:
struct Boo {
Boo () : z {}, l {} {}
Boo (int p1, int p2) : z {p1}, l {p2} {}
int z;
int l;
};
以下两个说明都按预期工作:
Foo<Boo> f_boo1 {1, 2, "A"};
Foo<Boo> f_boo2 {1, 2, "A", {1, 2}};
对我来说,没关系,我看不出有什么理由不向 class 添加两个构造函数,但如果类型不是我的怎么办?我应该用构造函数编写简单的包装器吗?
谢谢, 阿图尔
那是因为您正在尝试对 Boo
执行聚合初始化。见§8.5.4/3:
List-initialization of an object or reference of type
T
is defined as follows:— If
T
is an aggregate, aggregate initialization is performed (8.5.1).
您打算复制构造您的 Boo
...但实际上您正在进行聚合初始化,这导致尝试从 Boo
构造 int z
,因此错误
error: no viable conversion from 'Boo' to 'int'
请注意,您可以在没有任何模板的情况下用更少的代码重现您的问题:
Boo b;
Boo b2{b}; // error
修复很简单。只是不要使用列表初始化:
template <typename T>
struct Foo {
Foo (int n, int p, string s, T t = {})
: m_n {n}, m_p {p}, m_s {s}, m_t(t)
// ^^^^^^
{};