我在一个简单的向量类中使用auto,decltype和declval,以便执行基本的向量操作,例如添加标量和向量.但是,在尝试添加short类型和short类型的向量时,我无法使其工作.
// Vector.h
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
template<typename T> class Vector;
template<typename T> std::ostream& operator<< ( std::ostream& s, const Vector<T>& other );
template<typename T>
class Vector {
std::vector<T> base;
// vector + scalar
template<typename T1, typename T2>
friend auto operator+(const Vector<T1> & lhs, const T2 & scalar) -> Vector<decltype(std::declval<T1>() + std::declval<T2>())>;
friend std::ostream& operator<< <T> ( std::ostream& s, const Vector<T>& other );
public:
Vector();
Vector( const Vector<T>& other );
Vector<T>& operator= ( const …Run Code Online (Sandbox Code Playgroud)