Search code examples
c++11generic-programming

template argument list interconnected generic structs


I am attempting to write a small unit library to familiarize myself with generic programming and implicit conversion. I have chosen to organize the units into structs under their respective dimension. In order to implicitly convert between the units, a constructor is created for the compatible units. Before applying a template that is meant to allow any number type to occupy lengthValue, the code segment was happy. I now get the error below but I do not know how to approach resolving it.

#include <type_traits>

class Distance
{
  template<typename GenericNumber, typename = typenamestd::enable_if<std::is_arithmetic<GenericNumber>::value, GenericNumber>::type> struct feet;
  template<typename GenericNumber, typename = typename std::enable_if<std::is_arithmetic<GenericNumber>::value, GenericNumber>::type> struct inches;

  template<typename GenericNumber, typename = typename std::enable_if<std::is_arithmetic<GenericNumber>::value, GenericNumber>::type>
  struct feet
  {
  public:
    feet(GenericNumber _distance);
    feet(feet& _distance);
    feet(inches _distance);

    inline GenericNumber getLength() { return lengthValue; }
  private:
    GenericNumber lengthValue = 0.0;
  };

  template<typename GenericNumber, typename = typename std::enable_if<std::is_arithmetic<GenericNumber>::value, GenericNumber>::type>
  struct inches
  {
  public:
    inches(GenericNumber _distance);
    inches(feet _distance);
    inches(inches& _distance);

    inline GenericNumber getLength() { return lengthValue; }
  private:
    GenericNumber lengthValue = 0.0;
  };
};

errors:

Error   C2955 use of class template requires template argument list 

for lines

feet(inches _distance);
inches(feet _distance);

Solution

  • feet(inches<GenericNumber> _distance);
    inches(feet<GenericNumber> _distance);
    

    I needed to specify what type of the template I was referring to as a parameter, seems obvious now