为什么赢得这种类型转换工作?

时间:2018-03-20 17:19:52

标签: c++ c++11

鉴于此代码(基于自动分化类并消除了一些噪声):

template <int DIM=1>
struct number {
    number(double val)
        : val(val) {
            for (ssize_t ii=0; ii < DIM; ii++) {
                deriv[ii] = 0.0;
            }
        }       

     double val;
     double deriv[DIM];
};

template <int DIM>
static inline number<DIM> operator /(number<DIM> aa, number<DIM> bb) {
    return number<DIM>(aa/bb);
}

// Type your code here, or load an example.
int main() {
    1.0/number<>(2.0);
}

我收到错误:

main.cpp: In function 'int main()':
main.cpp:31:8: error: no match for 'operator/' (operand types are 'double' and 'number<>')
     1.0/number<>(2.0);
        ^
main.cpp:25:27: note: candidate: template number operator/(number, number)
 static inline number<DIM> operator /(number<DIM> aa, number<DIM> bb) {
                           ^
main.cpp:25:27: note:   template argument deduction/substitution failed:
main.cpp:31:21: note:   mismatched types 'number' and 'double'
     1.0/number<>(2.0);

为什么不将双倍转换为数字&lt;&gt;并允许操作通过?这是通过this在线工具编译的,但我也在另一台计算机上使用gcc4.7。

0 个答案:

没有答案