Question

I'm having trouble figuring out why my partially instantiated template isn't compiling in C++.

I'm trying to provide a class that returns different types based on the templates that are passed to it:

enum EVectorType {
  eVectorType_Scalar,
  eVectorType_Vector,
  eVectorType_Matrix
};

template<
  EVectorType kVectorTypeOne,
  EVectorType kVectorTypeTwo,
  typename TypeOne,
  typename TypeTwo>
class MultSwitch {
 private:
  const TypeOne &m_A;
  const TypeTwo &m_B;
 public:
  typedef TypeOne ResultType;
  MultSwitch(const TypeOne &a, const TypeTwo &b)
    : m_A(a), m_B(b) { }

  ResultType GetMultiplication() const { return m_A * m_B; }
};

The class is being used as an overload to the * operator:

template<typename T>
class VectorTraits {
  public:
  static const EVectorType kVectorType = eVectorType_Scalar;
};

template<typename T, const int N>
class VectorTraits<VectorBase<T, N> > {
 public:
  static const EVectorType kVectorType = eVectorType_Vector;
};

template<typename TypeOne, typename TypeTwo>
static inline 
  typename MultSwitch<
    VectorTraits<TypeOne>::kVectorType,
    VectorTraits<TypeTwo>::kVectorType,
    TypeOne, TypeTwo
  >::ResultType
operator*(const TypeOne &v1, const TypeTwo &v2) {
  typedef MultSwitch<
    VectorTraits<TypeOne>::kVectorType,
    VectorTraits<TypeTwo>::kVectorType,
    TypeOne, TypeTwo
  > VSwitch;
  return VSwitch(v1, v2).GetMultiplication();
}

The following specializations work as intended:

template<typename TypeOne, typename TypeTwo>
class MultSwitch<
  eVectorType_Scalar,
  eVectorType_Vector,
  TypeOne, TypeTwo> {
 private:
  const TypeOne &m_A;
  const TypeTwo &m_B;

 public:
  typedef TypeTwo ResultType;

  MultSwitch(const TypeOne &a, const TypeTwo &b)
    : m_A(a), m_B(b) { }

  ResultType GetMultiplication() const { return ScalarMultiply(m_B, m_A); }
};

template<typename TypeOne, typename TypeTwo>
class MultSwitch<
  eVectorType_Vector,
  eVectorType_Scalar,
  TypeOne, TypeTwo> {
 private:
  const TypeOne &m_A;
  const TypeTwo &m_B;

 public:
  typedef TypeOne ResultType;

  MultSwitch(const TypeOne &a, const TypeTwo &b)
    : m_A(a), m_B(b) { }

  ResultType GetMultiplication() const { return ScalarMultiply(m_A, m_B); }
};

template<typename TypeOne, typename TypeTwo>
class MultSwitch<
  eVectorType_Vector,
  eVectorType_Vector,
  TypeOne, TypeTwo> {
 private:
  const TypeOne &m_A;
  const TypeTwo &m_B;

 public:
  typedef typename TypeOne::ScalarType ResultType;

  MultSwitch(const TypeOne &a, const TypeTwo &b)
    : m_A(a), m_B(b) { }

  ResultType GetMultiplication() const { return m_A.Dot(m_B); }
};

However, the following does not:

template<typename TypeOne, typename TypeTwo>
class MultSwitch<
  eVectorType_Matrix,
  eVectorType_Matrix,
  TypeOne, TypeTwo> {
 private:
  const TypeOne &m_A;
  const TypeTwo &m_B;

 public:
  typedef MatrixBase<typename TypeOne::ScalarType, TypeOne::kNumRows, TypeTwo::kNumCols> ResultType;

  MultSwitch(const TypeOne &a, const TypeTwo &b)
    : m_A(a), m_B(b) { }

  ResultType GetMultiplication() const { return m_A.MultiplyMatrix(m_B); }
};

Whenever I write an expression a * b where the type traits of a and b match eVectorType_Matrix, for some reason, the compiler says that ResultType is the same type as TypeOne, or a. I have changed the various different typedefs in the working specializations, but they all seem to produce the same error.

More specifically, the following code:

template <typename T, const int nRows, const int nCols>
class MatrixBase {
 public:
  typedef T ScalarType;
  static const int kNumRows = nRows;
  static const int kNumCols = nRows;
  ...
};

template<typename T, const int N, const int M>
class VectorTraits<MatrixBase<T, N, M> > {
 public:
  static const EVectorType kVectorType = eVectorType_Matrix;
};

MatrixBase<int, 2, 3> a;
...

MatrixBase<int, 3, 5> b;
...

MatrixBase<float, 2, 5> amb = a * b;

Produces the following error:

TestMatrix.cpp:145:42: error: conversion from ‘MultSwitch<(EVectorType)2u, (EVectorType)2u, MatrixBase<int, 2, 3>, MatrixBase<int, 3, 5> >::ResultType {aka MatrixBase<int, 2, 3>}’ to non-scalar type ‘MatrixBase<float, 2, 5>’ requested
   MatrixBase<float, 2, 5> amb = a * b;
Was it helpful?

Solution

I believe you have two problems:

  1. In MatrixBase, I think

    static const int kNumCols = nRows;
    

    should be

    static const int kNumCols = nCols;
    
  2. "a * b" will return a

    MatrixBase<int, 2, 5>
    

    type, not

    MatrixBase<float, 2, 5>
    

    You'll need to add a copy constructor to perform the int->float conversion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top