小编bob*_*ter的帖子

当操作数类型为short时,编译模板标量向量加法运算符失败

我在一个简单的向量类中使用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)

c++ templates decltype auto c++14

2
推荐指数
1
解决办法
552
查看次数

标签 统计

auto ×1

c++ ×1

c++14 ×1

decltype ×1

templates ×1