仅在类型为std :: complex时缩放

问题描述 投票:1回答:1

我具有以下模板功能:

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

以下对函数的调用起作用:

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

但是,当我以以下方式调用该函数时,出现错误“标量初始化程序中的多余元素。

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

基本上,如果参数类型为std :: complex,我想进行缩放。该如何解决呢? std :: cout <

c++ c++11 templates
1个回答
2
投票

由于行const T y(std::cos(theta),-1*std::sin(theta));仍然需要有效,因此无法编译该函数;不是T = float

如果使用C ++ 17的constexpr,这将很容易解决:

© www.soinside.com 2019 - 2024. All rights reserved.