仅为专用模板类定义转换运算符

时间:2016-08-21 09:48:56

标签: c++ templates

我想为矩阵< 1,1>定义转换为float。我无法弄清楚如何实际定义它。如果我把它作为一个全局函数

template<typename T>
inline operator T(const matrix<T, 1, 1> &m){ return m(0, 0); }

我得到“operator ..必须是非静态成员函数”

我当然可以将其定义为通用矩阵模板的成员,但随后将为所有矩阵定义 - 这不是我想要的。我希望它仅针对1x1矩阵的特定情况进行定义。

3 个答案:

答案 0 :(得分:4)

你必须为此专门化一个类,例如:

template <typename Base, typename T, std::size_t W, std::size_t H>
struct MatrixConversion
{ /*Empty*/ };

template <typename Base, typename T> struct MatrixConversion<T, 1u, 1u>
{
    operator const T&() const { return static_cast<const Base&>(*this).m[0][0]; }
};


template <typename T, std::size_t W, std::size_t H>
struct Matrix : MatrixConversion<Matrix<T, W, H>, T, W, H>
{
    // Your code
};

答案 1 :(得分:1)

组合加专业化将是最易于维护的方法。

您没有指定矩阵模板类中的维数,因此我假设它可以是可变参数。

#include <cstdint>
#include <utility>

//
// forward-declare class template for convenience.
//
template<class T, std::size_t...Dimensions>
struct matrix;

//
// classes to figure out the storage requirements of a multi-dimensional
// matrix
//
template<class T, std::size_t...Dimensions> struct storage;
template<class T, std::size_t N>
  struct storage<T, N>
  {
    using type = T[N];
  };

template<class T, std::size_t...Rest, std::size_t N>
  struct storage<T, N, Rest...>
  {
    using less_dimension_type = typename storage<T, Rest...>::type;
    using type = less_dimension_type[N];
  };


//
// functions for dereferencing multi-dimensional arrays
//
template<class Array, class Arg>
decltype(auto) deref(Array& array, Arg&& arg)
{
  return array[arg];
}

template<class Array, class Arg, class Arg2>
decltype(auto) deref(Array& array, Arg&& arg, Arg2&& arg2)
{
  return array[arg][arg2];
}

template<class Array, class Arg, class...Args>
decltype(auto) deref(Array& array, Arg&& arg, Args&&...args)
{
  return deref(deref(array, arg), std::forward<Args>(args)...);
}

//
// prototype for operations we want to conditionally apply
//
template<class Matrix>
struct matrix_conditional_ops
{
  // in the general case, none
};

//
// compose the matrix class from conditional_ops<>
//    
template<class T, std::size_t...Dimensions>
struct matrix 
  : matrix_conditional_ops<matrix<T, Dimensions...>>
{

    template<class...Dims>
    decltype(auto) at(Dims&&...ds)
    {
      return deref(_data, std::forward<Dims>(ds)...);
    }

    template<class...Dims>
    decltype(auto) at(Dims&&...ds) const
    {
      return deref(_data, std::forward<Dims>(ds)...);
    }

   typename storage<T, Dimensions...>::type _data;
};

//
// define the condition operations for the <T, 1, 1> case
//    
template<class T>
  struct matrix_conditional_ops<matrix<T, 1, 1>>
  {
    using matrix_type = matrix<T, 1, 1>;

    operator T const() { return static_cast<matrix_type const&>(*this).at(0,0); }
  };


int main()
{
  matrix<double, 1, 1> m11;

  m11.at(0,0) = 6.0;
  double d = m11;

  matrix<double, 2, 2> m22;
  // compile error:
//  double d2 = m22;

  // bonus points:

  matrix<double, 3, 5, 2, 7> mxx;
  mxx.at(2, 4, 1, 6) = 4.3;  // probably needs some compile-time checking...

}

有人可能想检查我的阵列打包/解除引用的逻辑......

答案 2 :(得分:0)

在我看来,

JarodRichard已经给了你最好的答案,它们可以很好地适应任何数量的限制运营商。

但是,如果您无法重新设计课程,或者您需要的只是一个快速而肮脏的opertor T(),您可以使用以下内容

template<typename T, std::size_t N1, std::size_t N2>
struct Matrix
{
    T m[N1][N1];

    operator T()
    {
        static_assert(N1 == 1 && N2 == 1, "Only applicable to scalars");
        return m[0][0];
    }
};

哪个是live here

相关问题