浮点和整数歧义
我有一个函数(和一个构造函数)应该能够获取整数和浮点值。事实上,我希望它采用 int64_t
或 long double
,所以我想要的是,
class Foo {
public:
Foo(int64_t value=0);
Foo(long double value);
};
但是如果我这样做并尝试 Foo f = 1;< /code> 编译器抱怨从
int
到 Foo
的转换不明确。好的,但是如果我将第一个构造函数更改为采用 int32_t
,就不会有这样的歧义。谁能向我解释为什么会这样。
I have a function (and a constructor) that should be able to take integer and floating point values. In fact I want it to take an int64_t
or a long double
, so what I want is,
class Foo {
public:
Foo(int64_t value=0);
Foo(long double value);
};
However if I do this and try Foo f = 1;
the compiler complains about the conversion from int
to Foo
being ambiguous. Ok, but if I change the first constructor to take a int32_t
there is no such ambiguity. Can anyone explain to me why this is the case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1 文字的类型是 int。任一构造函数都需要转换,int 到 int64_t 或 int 到 long double。编译器认为它们中的任何一个都不是更好的,所以它会抱怨。通过添加 Foo(int) 构造函数来解决它。或者转换文字,例如 (int64_t)1。
The type of the 1 literal is int. Either constructor is going to need a conversion, int to int64_t vs int to long double. The compiler doesn't think either of them is preferable so it complains. Solve it by adding a Foo(int) constructor. Or casting the literal, like (int64_t)1.