根据C++ ISO规范,§26.2/ 2:
将模板实例化为
complex除float(double或未long double指定)之外的任何类型的效果.
为什么标准作者会明确添加此限制?这使得它没有被指定,例如,如果你制造complex<int>或者complex<MyCustomFixedPointType>看起来像是人为限制会发生什么.
有这个限制的原因吗?如果要complex使用自己的自定义类型进行实例化,是否有解决方法?
我主要是问这个问题是因为早先的问题,其中OP对为什么abs给出奇怪的输出感到困惑complex<int>.也就是说,考虑到我们也可能想要用complex固定点类型,更高精度的实数等来制作数字,这仍然没有意义.
class Foo {
public:
Foo(float b) {}
};
class Bar {
public:
Bar(Foo foo) {}
};
int main(int argc, char *argv[]) {
Bar b1(3.0f); // accept, one implicit convertion happens there.
Bar b2 = 3.0f; // error: no viable conversion from 'float' to 'Bar'
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么第二个表达式无法编译?我希望它会调用与第一个表达式相同的转换构造函数.