mardi 20 mars 2018

Why won't this type conversion work?

Given this code (based on an autodifferentiation class with some noise stripped out):

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);
}

I get an error:

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);

Why won't it convert double to a number<> and allow the operation to go through? This was compiled via this online tool, but I'm using gcc4.7 on another computer too.

Aucun commentaire:

Enregistrer un commentaire