2016-07-23 159 views
1

我想编写我自己的Vector类模板,并且还想添加一些专业化,例如可以通过x/y/z访问组件的3D矢量类型。C++模板专业化冗余减少

到目前为止,模板和专业化工作状况良好,但问题是,专用模板需要从基本模板进行大量复制/粘贴才能正常工作。我想减少这一点。

这是它看起来像现在:

template<class T, unsigned int dim> 
class Vector; 

template<class T, unsigned int dim> 
Vector<T, dim> add(Vector<T, dim> const& lhs, Vector<T, dim> const& rhs) 
{ 
    Vector<T, dim> tmp; 
    for (unsigned int i = 0; i < dim; ++i) 
    { 
     tmp[i] = lhs[i] + rhs[i]; 
    } 

    return tmp; 
} 

template<class T, unsigned int dim, class S> 
Vector<T, dim> add(Vector<T, dim> const& lhs, S const& rhs) 
{ 
    Vector<T, dim> tmp; 
    for (unsigned int i = 0; i < dim; ++i) 
    { 
     tmp[i] = lhs[i] + rhs; 
    } 

    return tmp; 
} 

template<class T, unsigned int dim> 
Vector<T, dim> operator+(Vector<T, dim> const& lhs, Vector<T, dim> const& rhs) 
{ 
    return vectors::add(lhs, rhs); 
} 

template<class T, unsigned int dim, class S> 
Vector<T, dim> operator+(Vector<T, dim> const& lhs, S const& rhs) 
{ 
    return vectors::add(lhs, rhs); 
} 

template<class T, unsigned int dim> 
class Vector 
{ 
//... 
protected: 
    T values[dim] __attribute((aligned(16))); 
public: 
    template<class R, unsigned int fdim> 
    friend Vector<R, fdim> operator+(Vector<R, fdim> const& lhs, Vector<R, fdim> const& rhs); 
    template<class R, unsigned int fdim, class S> 
    friend Vector<R, fdim> operator+(Vector<R, fdim> const& lhs, S const& rhs); 
    template<class R, unsigned int fdim, class S> 
    friend Vector<R, fdim> operator+(S const& lhs, Vector<R, fdim> const& rhs); 
//... 
//constructors, etc. 
}; 

template<class T> 
class Vector<T, 3> 
{ 
//... 
protected: 
    T values[3] __attribute((aligned(16))); 
public: 
    T& x = values[0]; 
    T& y = values[1]; 
    T& z = values[2]; 

    //lots of copy-pasta :(
    template<class R, unsigned int fdim> 
    friend Vector<R, fdim> operator+(Vector<R, fdim> const& lhs, Vector<R, fdim> const& rhs); 
    template<class R, unsigned int fdim, class S> 
    friend Vector<R, fdim> operator+(Vector<R, fdim> const& lhs, S const& rhs); 
    template<class R, unsigned int fdim, class S> 
    friend Vector<R, fdim> operator+(S const& lhs, Vector<R, fdim> const& rhs); 
//... 
//constructors, etc. 
}; 

现在我认为最简单的办法是简单地定义Vector3D作为一个子类Vector模板的,就像这样:

template<class T> 
class Vector3D: public Vector<T, 3> 
{ 
//... 
public: 
    T& x = values[0]; 
    T& y = values[1]; 
    T& z = values[2]; 

    //no copy-pasta :) 
//... 
//constructors, etc. 
}; 

这并不在所有的工作中,由于不确定性:

ambiguous overload for ‘operator+’ (operand types are ‘const vec3f {aka const math::vectors::Vector3D<float>}’ and ‘math::vectors::vec3f {aka math::vectors::Vector3D<float>}’) 
../main.cpp:84:16: note: candidates are: 
In file included from ../main.cpp:10:0: 
../include/vector.hpp:720:16: note: math::vectors::Vector<T, dim> math::vectors::operator+(const math::vectors::Vector<T, dim>&, const math::vectors::Vector<T, dim>&) [with T = float; unsigned int dim = 3u] 
Vector<T, dim> operator+(Vector<T, dim> const& lhs, Vector<T, dim> const& rhs) 
       ^
../include/vector.hpp:726:16: note: math::vectors::Vector<T, dim> math::vectors::operator+(const math::vectors::Vector<T, dim>&, const S&) [with T = float; unsigned int dim = 3u; S = math::vectors::Vector3D<float>] 
Vector<T, dim> operator+(Vector<T, dim> const& lhs, S const& rhs) 
       ^
../include/vector.hpp:732:16: note: math::vectors::Vector<T, dim> math::vectors::operator+(const S&, const math::vectors::Vector<T, dim>&) [with T = float; unsigned int dim = 3u; S = math::vectors::Vector3D<float>] 
Vector<T, dim> operator+(S const& lhs, Vector<T, dim> const& rhs) 

因此,看起来模板替换失败了,因为S也可以被新的Vector3D类替代,而它应该只处理标量。

于是,我就写了标量小包装类,像这样摆脱这个问题的:

template<class T> 
class ScalarType 
{ 
public: 
    T value; 
    ScalarType() : 
      value(0) 
    { 

    } 

    ScalarType(T const& _v) : 
      value(_v) 
    { 

    } 

    ScalarType(ScalarType<T> const& rhs) : 
      value(rhs.value) 
    { 

    } 

    operator T&() 
    { 
     return value; 
    } 

    operator T() const 
    { 
     return value; 
    } 
}; 

而且随着ScalarType<S> const& (l|r)hs取代的S const& (l|r)hs所有实例。

让操作符在两侧再次运行Vector,但是应该处理Vector-Scalar操作的操作符仍然失败。

这次是因为标量值必须显式为ScalarType,因为对其的隐式转换不适用于模板替换。

那么,有什么办法让这个工作或我必须坚持复制粘贴代码?

+0

这是可能实现http://stackoverflow.com/questions/31449433/ generic-c-multidimensional-iterators/38510047#38510047 – slavanap

+0

你做得太多了。有一个基类向量(值元组),并用循环实现操作(使用模板参数'dim')。然后(仅当性能和(!)编译器生成的程序集不够好时),请调整专门的模板。在任何情况下,都要在专门的模板中保留专门的功能(不要混淆基类) –

+0

@DieterLücking,这就是计划。这些操作符根本不是专用的,只是Vector类本身,而且仅仅是因为我想通过Vector3D测试等简单引用来访问矢量组件; test.x = 12; 但是,当我只是专注于正常的方式,我不得不重新定义每个操作符超载或使用上面的朋友函数。 –

回答

1

在这里完成部分模板专业化和CRTP。

maybe_has_z<Container, N>是转化成Container::z()一个Container::operator[](2)类,但只有当Container::size() >= 3

#include <array> 
#include <iostream> 
#include <algorithm> 

// 
// some boilerplate - note the different indecies 
// 

// define some concepts 

template<class Container, std::size_t N, typename= void> 
struct maybe_has_x{}; 

template<class Container, std::size_t N, typename = void> 
struct maybe_has_y{}; 

template<class Container, std::size_t N, typename = void> 
struct maybe_has_z{}; 

// specialise the concepts into (sometimes) concrete accessors 

template<class Container, std::size_t N> 
struct maybe_has_x<Container, N, std::enable_if_t<(N > 0)>> 
{ 
    auto& x() const { return static_cast<const Container&>(*this)[0]; } 
    auto& x() { return static_cast<Container&>(*this)[0]; } 
}; 

template<class Container, std::size_t N> 
struct maybe_has_y<Container, N, std::enable_if_t<(N > 1)>> 
{ 
    auto& y() const { return static_cast<const Container&>(*this)[1]; } 
    auto& y() { return static_cast<Container&>(*this)[1]; } 
}; 

template<class Container, std::size_t N> 
struct maybe_has_z<Container, N, std::enable_if_t<(N > 2)>> 
{ 
    auto& z() const { return static_cast<const Container&>(*this)[2]; } 
    auto& z() { return static_cast<Container&>(*this)[2]; } 
}; 

// define our vector type 

template<class T, std::size_t N> 
struct Vector 
: std::array<T, N> 
, maybe_has_x<Vector<T, N>, N> // include the maybe_ concepts 
, maybe_has_y<Vector<T, N>, N> 
, maybe_has_z<Vector<T, N>, N> 
{ 
private: 
    using inherited = std::array<T, N>; 
public: 
    Vector() : inherited {} {}; 
    Vector(std::initializer_list<T> il) 
    : inherited { } 
    { 
     std::copy_n(il.begin(), std::min(il.size(), this->size()), std::begin(*this)); 
    } 
    Vector(const inherited& rhs) : inherited(rhs) {} 

public: 
    using value_type = typename inherited::value_type; 

    // offer arithmetic unary functions in class (example +=) 
    // note that this allows us to add integers to a vector of doubles 
    template<class Other, std::enable_if_t<std::is_convertible<value_type, Other>::value> * = nullptr> 
    Vector& operator+=(const Vector<Other, N>&rhs) { 
     auto lfirst = std::begin(*this); 
     auto rfirst = std::begin(rhs); 
     auto lend = std::end(*this); 
     while (lfirst != lend) { 
      *lfirst += *rfirst; 
      ++lfirst; 
      ++rfirst; 
     } 
     return *this; 
    } 

}; 

// offer binary arithmetic as free functions 

template<class T, std::size_t N, class Other> 
Vector<T, N> operator+(Vector<T, N> lhs, const Vector<Other, N>& rhs) { 
    lhs += rhs; 
    return lhs; 
} 

// offer some streaming capability 

template<class T, std::size_t N> 
std::ostream& operator<<(std::ostream& os, const Vector<T, N>& rhs) { 
    auto sep = ""; 
    os << '['; 
    for (auto& x : rhs) { 
     os << sep << x; 
     sep = ", "; 
    } 
    return os << ']'; 
} 

// test 

int main() 
{ 
    auto a = Vector<double, 3> { 2.1, 1.2, 3.3 }; 
    auto b = a + a + Vector<int, 3> { 1, 1, 1 }; 
    std::cout << a << std::endl; 
    std::cout << b << std::endl; 

    std::cout << a.x() << ", " << a.y() << ", " << a.z() << std::endl; 

    auto c = Vector<double, 2> { 4.4, 5.5 }; 
    std::cout << c << std::endl; 

    std::cout << c.x() << std::endl; 
    std::cout << c.y() << std::endl; 
    // won't compile 
    // std::cout << c.z() << std::endl; 
} 

预期输出:

[2.1, 1.2, 3.3] 
[5.2, 3.4, 7.6] 
2.1, 1.2, 3.3 
[4.4, 5.5] 
4.4 
5.5 
+0

恩,除非我错过了一些东西,那怎么解决陈述的问题呢? –

+0

@pfannkuchen_gesicht哦,是的,将这个问题误解为多维向量。道歉。将更新 –

+0

@pfannkuchen_gesicht用正确答案更新。我很抱歉。 –