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

bob*_*ter 2 c++ templates decltype auto c++14

我在一个简单的向量类中使用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 Vector<T> &other );
  auto& operator[] ( int i );
  void insert( const T element );
};

template<typename T>
Vector<T>::Vector() {
}

template<typename T>
Vector<T>::Vector( const Vector<T>& other ) {
  base = other.base;
}

template<typename T>
Vector<T>& Vector<T>::operator= ( const Vector<T> &other ) {
  base = other.base;
}

template<typename T>
auto& Vector<T>::operator[] ( int i ) {
  assert( i >= 0 && i < base.size() );
  return base[i];
}

template<typename T>
void Vector<T>::insert( const T element ) {
  base.push_back( element );
}

// vector + scalar
template<typename T1, typename T2>
auto operator+(const Vector<T1> & lhs, const T2 & scalar)
  -> Vector<decltype(std::declval<T1>() + std::declval<T2>())>
{
  typedef decltype(std::declval<T1>() + std::declval<T2>()) T3;
  Vector<T3> result;
  result.base.reserve(lhs.base.size());
  std::transform(lhs.base.begin(), lhs.base.end(), std::back_inserter(result.base),
                 [&scalar](const T1 & element) { return element + scalar; });
  return result;
}
Run Code Online (Sandbox Code Playgroud)

测试程序:

// vector_test.cpp

void test_vector_int_scalar_int_addition() {
  Vector<int> v;
  v.insert( 1 );
  v.insert( 2 );
  v.insert( 3 );
  int s = 2;
  Vector<int> res = v + s;
  }

void test_vector_short_scalar_short_addition() {
   Vector<short> v;
   v.insert( 1 );
   v.insert( 2 );
   v.insert( 3 );
   short s = 2;
   Vector<short> res = v + s;
}

int main() {
   test_vector_int_scalar_int_addition(); // Compiles and works
   test_vector_short_scalar_short_addition(); // Does NOT compile
}
Run Code Online (Sandbox Code Playgroud)

使用简短的"test_vector_short_scalar_short_addition()"来编译上述内容失败,但在使用int"test_vector_int_scalar_int_addition()"时工作正常.我收到以下错误:

 error: no viable conversion from 'Vector<decltype(std::declval<short>() + std::declval<short>())>' to 'Vector<short>'

让我感到困惑的是,对于其他类型,例如double,float,long等,它可以正常工作,但只有在使用short时才会出现故障.

编译器信息:

clang++ --version
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Run Code Online (Sandbox Code Playgroud)

bog*_*dan 5

内置加法运算符的操作数经历了通常的算术转换,其中包括整数提升,这意味着大多数整数转换等级的整数类型int被转换为int.

以上意味着你Vector<decltype(std::declval<short>() + std::declval<short>())>实际上是Vector<int>,并且编译器抱怨它无法将其转换为Vector<short>.

这可以使用以下代码进行验证:

#include <iostream>
#include <type_traits>

int main()
{
    std::cout << std::is_same<decltype(std::declval<short>() + std::declval<short>()), int>::value << '\n';
}
Run Code Online (Sandbox Code Playgroud)

打印1.


作为确定结果类型的替代方法,您可以考虑使用std :: common_type.这将给你一个shortif两个操作数short,但如果类型不同,仍然会应用通常的算术转换 - 例如,unsigned short并且short通常会产生int(如果sizeof(short) < sizeof(int)).