mercredi 29 avril 2020

Type conversion from std::complex

I have a class MyType that implements a user-defined arithmetic type. This class provides the following conversion operator

struct MyType 
{ ...
  operator double()
  { 
    return to_double(); // This converts my type to a double value
  }
... };

Using this class as follows works fine:

double d = MyType(1);

However, using this class as type within std::complex, e.g.

#include <complex>
std::complex<double> c = std::complex<MyType>(1,1);

fails with the following compiler error:

error: conversion from 'std::complex<MyType>' to non-scalar type 'std::complex<double>' requested

Any help to solve this problem is appreciated.

Matthias

Aucun commentaire:

Enregistrer un commentaire