jeudi 4 juin 2020

Scale only if the type is std::complex

I have the following template function:

template <typename T0>
struct ComplexTypeTraits
{
    using type = T0;
};

template <typename T0>
struct ComplexTypeTraits<std::complex<T0> >
{
    using type = T0;
};
template<typename T>
void myFunction(T& out, const T & in)
{
    using T1 = typename ComplexTypeTraits<T>::type; // will be 'T' for std::complex<T>

    out = in;
    if(std::is_same<T, T1>::value == false)
    {// if the type is std::complex<T>, do a scaling
        const T1 theta = M_PI/2.0;
        const T y(std::cos(theta),-1*std::sin(theta));
        out = out*y;
    }
}

The following call to the function works:

    std::complex<float> in(10, 5);
    std::complex<float> out = 0;
    myFunction<std::complex<float>>(out, in);
    std::cout<<"out is: "<<out<<std::endl;

However, when I call the function in the following way, is gives the error "excess elements in scalar initializer.

float in = 10;
float out = 0;
myFunction<float>(out, in);

Basically, I would like to do a scaling, if the argument type is std::complex. How to solve the issue? std::cout<<"out is: "<

Aucun commentaire:

Enregistrer un commentaire