fork(1) download
  1. using namespace std;
  2.  
  3. template <typename IntType, IntType MIN_VAL, IntType MAX_VAL>
  4. struct BoundInt
  5. {
  6. static constexpr IntType MIN = MIN_VAL;
  7. static constexpr IntType MAX = MAX_VAL;
  8.  
  9. IntType value;
  10. };
  11.  
  12. template <typename T> struct ConversionTraits;
  13.  
  14. template <typename T>
  15. struct Value
  16. {
  17. // Pointless for the sake of this example
  18. void convert()
  19. {
  20. ConversionTraits<T>::convert();
  21. }
  22.  
  23. T value;
  24. };
  25.  
  26. // this 'implementation' is also pointless for example purposes
  27. struct ConvertImpl
  28. {
  29. static void convert() { }
  30. };
  31. template <> struct ConversionTraits<int> : public ConvertImpl {};
  32.  
  33. // This is my problem. How do I partially specialise for something that has
  34. // constants as template parameters.
  35. template <typename IntType>
  36. struct ConversionTraits< BoundInt<IntType, IntType, IntType> >
  37. {
  38. static void convert() {}
  39. };
  40.  
  41. int main()
  42. {
  43. Value<int> intval;
  44. intval.convert();
  45.  
  46. Value<BoundInt<unsigned, 0, 100>> value;
  47. value.convert();
  48. }
  49.  
Compilation error #stdin compilation error #stdout 0s 3336KB
stdin
Standard input is empty
compilation info
prog.cpp:36:60: error: type/value mismatch at argument 2 in template parameter list for ‘template<class IntType, IntType MIN_VAL, IntType MAX_VAL> struct BoundInt’
 struct ConversionTraits< BoundInt<IntType, IntType, IntType> >
                                                            ^
prog.cpp:36:60: error:   expected a constant of type ‘IntType’, got ‘IntType’
prog.cpp:36:60: error: type/value mismatch at argument 3 in template parameter list for ‘template<class IntType, IntType MIN_VAL, IntType MAX_VAL> struct BoundInt’
prog.cpp:36:60: error:   expected a constant of type ‘IntType’, got ‘IntType’
prog.cpp:36:62: error: template argument 1 is invalid
 struct ConversionTraits< BoundInt<IntType, IntType, IntType> >
                                                              ^
prog.cpp: In instantiation of ‘void Value<T>::convert() [with T = BoundInt<unsigned int, 0u, 100u>]’:
prog.cpp:47:16:   required from here
prog.cpp:20:32: error: incomplete type ‘ConversionTraits<BoundInt<unsigned int, 0u, 100u> >’ used in nested name specifier
   ConversionTraits<T>::convert();
                                ^
stdout
Standard output is empty