Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;模板-推广双精度和复精度的标量积<;双倍>;载体_C++_Templates_Vector_Double_Complex Numbers - Fatal编程技术网

C++ C++;模板-推广双精度和复精度的标量积<;双倍>;载体

C++ C++;模板-推广双精度和复精度的标量积<;双倍>;载体,c++,templates,vector,double,complex-numbers,C++,Templates,Vector,Double,Complex Numbers,我正在尝试重载*运算符,以便使用模板计算向量和向量的标量积,在尝试了我所知道的一切之后,我得出了这个结论: #include <iostream> #include <complex> #include <vector> using namespace std; template<typename T> T operator* (const vector<T> &a, const vector<T> &b)

我正在尝试重载
*
运算符,以便使用模板计算
向量
向量
的标量积,在尝试了我所知道的一切之后,我得出了这个结论:

#include <iostream>
#include <complex>
#include <vector>
using namespace std;

template<typename T> T operator* (const vector<T> &a, const vector<T> &b) {
    T retvar; complex<double> c = 0;
    for (int i = 0; i < b.size(); i++) c += conj(a[i])*b[i];
    if (is_same<T, double>::value) retvar = c.real();
    else                           retvar = c;  // (*) the error is generated by this line
    return retvar;
}

int main() {
    vector<double> a, b;
    a.push_back(5); b.push_back(3);
    cout << a*b << endl;
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
模板T运算符*(常数向量和a、常数向量和b){
复c=0;
对于(inti=0;i不要重载你不拥有的类型的运算符。相反,你可以编写一个

template <typename T>
struct my_vect {
    std::vector<T> data;
    T operator* (const my_vect<T>& other);
};
现在,对于复向量和非复向量,标量积的定义可以相同。完整示例:

#include <vector>
#include <complex>
#include <iostream>
#include <type_traits>

template <typename T> struct is_complex : std::false_type {};
template <typename T> struct is_complex<std::complex<T>> :std::true_type {};


template <typename T>
T my_conj(const T&t) {
    if constexpr (is_complex<T>::value) return std::conj(t);
    else return t;
}


template <typename T>
struct my_vect {
    std::vector<T> data;
    T operator* (const my_vect<T>& other) {
        T result;
        for (size_t i = 0; i < data.size(); i++) result += my_conj(data[i])*other.data[i];
        return result;
    }
};

int main() {
   my_vect<double> x{{1,2,3}};
   std::cout << x*x << '\n';
   my_vect<std::complex<double>> y{{ {1,2} }};
   std::cout << y*y;
}
#包括
#包括
#包括
#包括
模板结构是复杂的:std::false_类型{};
模板结构是复杂的:std::true\u类型{};
模板
T my_conj(施工T&T){
如果constexpr(is_complex::value)返回std::conj(t);
否则返回t;
}
模板
构造我的向量{
std::矢量数据;
T运算符*(常数我的向量和其他){
T结果;
对于(size_t i=0;istd::我能相信使用
auto
和/或尾部返回类型是你应该研究的。对于double和complex,标量积不是一样吗?我的意思是,除了一个是double,另一个是complex?不要对你不拥有的类型重载操作。如果你想,在你自己的类中包装向量,并为r他们。同意@NathanOliver。使用显式命名的函数-
scalar\u product
。您使用的是c++17吗?如果constexpr(是相同的…)则使用
。谢谢你,我想这会有用的,但是在你回答之前的几分钟,有人已经留下了一条评论,实际上解决了所有问题。@marco我看到了评论,但是
是相同的
单独检查只能检查
双重
,而检查
复杂的专门化
适用于任何复杂或复杂的东西不
template <typename T> struct is_complex : std::false_type {};
template <typename T> struct is_complex<std::complex<T>> :std::true_type {};


template <typename T>
T my_conj(const T&t) {
    if constexpr (is_complex<T>::value) return std::conj(t);
    else return t;
}
#include <vector>
#include <complex>
#include <iostream>
#include <type_traits>

template <typename T> struct is_complex : std::false_type {};
template <typename T> struct is_complex<std::complex<T>> :std::true_type {};


template <typename T>
T my_conj(const T&t) {
    if constexpr (is_complex<T>::value) return std::conj(t);
    else return t;
}


template <typename T>
struct my_vect {
    std::vector<T> data;
    T operator* (const my_vect<T>& other) {
        T result;
        for (size_t i = 0; i < data.size(); i++) result += my_conj(data[i])*other.data[i];
        return result;
    }
};

int main() {
   my_vect<double> x{{1,2,3}};
   std::cout << x*x << '\n';
   my_vect<std::complex<double>> y{{ {1,2} }};
   std::cout << y*y;
}