lundi 25 février 2019

How to avoid "template parameters not deducible in partial specialization"

I would like to have access to a nested class using templates, and can't figure out how to do it: A sample code:

template <typename T> class mything {
  typedef unsigned key;
  T data;
public:
  template <typename subT> class mysubthing { typedef subT value_type; };
  using subthing = mysubthing<T>;
  using subthing_ro = mysubthing<const T>;
};

template<typename T> struct X; // a container for all value_types

#ifdef MAKE_IT_FAIL
// this should automatically set the X<mything<T>::mysubthing<subT>
template<typename T,typename subT> struct X<typename mything<T>::template mysubthing<subT>> {
  using value_type = subT;
};
#endif

typedef mything<int> intthing;

#ifndef MAKE_IT_FAIL
template<> struct X<mything<int>::subthing> { using value_type = int; };
template<> struct X<mything<int>::subthing_ro> { using value_type = const int; };
#endif

int main(void) {
  intthing t;
  X<intthing::subthing>::value_type data = 1; // a data object
  X<intthing::subthing_ro>::value_type data_ro = 1; // read-only data object

  return 0;
}

This compiles without -DMAKE_IT_FAIL, but of course it completely misses the point about templates, since what I wanted was entered manually. How can I make it work with -DMAKE_IT_FAIL?

Aucun commentaire:

Enregistrer un commentaire