【发布时间】:2016-01-22 21:02:26
【问题描述】:
我将提供这两个构造函数。
BigUnsigned(int value)
:BigUnsigned(static_cast<unsigned long long>(value)){
}
BigUnsigned(unsigned long long value);
问题是某些参数值的调用不明确。根据this answer,指的是C++11标准,
整数字面量的类型是对应列表的第一个 在表6中可以表示其值。
表 6 在这里
int
long int
long long int
因此,在我的例子中,构造函数参数的类型(整数文字)如果它属于范围......
<0, numeric_limits<int>::max()> 是int
---> 调用 BigUnsigned(int)
(numeric_limits<int>::max(), numeric_limits<long int>::max()> 是long int
--->模棱两可
(numeric_limits<long int>::max(), too big literal) 是long long int
--->模棱两可
如何在不声明更多构造函数或显式类型转换参数的情况下解决歧义?
This question on integer promotion 和 integer conversion 可能有用。不过,我仍然不知道其中哪一个适用于我的情况。
【问题讨论】:
-
您需要
int构造函数吗?BigUnsigned(unsigned long long value);不是涵盖了你想要的一切吗? -
@NathanOliver 似乎确实如此。但是,分配迫使我实现这两个构造函数,可能是为了解决这类问题:)。
int参数应与位级别上的unsigned long long完全相同。 -
显式好,隐式不好。添加一个 tie-breaker 第一个参数,如果可能的话,使用一些自我描述的名称。如果无法设计一个合理的名称,那么这个设计就是不好的,必须扔到最近的火上。
-
@Cheersandhth.-Alf 我不允许更改公共界面。
-
投票关闭,因为不清楚您的要求。
标签: c++ c++11 integer type-conversion integer-promotion