I have this chunk of code that is a common pattern to do template specialization. To remove the requirement of specifying DataType1
for Processor1<DataType1>
in the first line of main function, I want to receive a template template argument instead. Looks like the using
directive does not support assigning an "open" template argument and I'm unable to find any example of that in the web (maybe I'm not searching with the appropriate keywords...)
#include <iostream>
struct DataType1 {};
struct DataType2 {};
template<class TDataType>
struct BaseMessage{ using DataType = TDataType; };
template<class TDataType>
struct ChildMessage : public BaseMessage<TDataType> {};
template<class TDataType>
struct Processor1 {
static void func() { std::cout << "Processor1" << std::endl; }
};
template<class TDataType>
struct Processor2 {
static void func() { std::cout << "Processor2" << std::endl; }
};
template<class TResult>
struct FindDefaultProcessor;
template<>
struct FindDefaultProcessor<DataType1>
{
using type = Processor1;
};
template<class TResult, template<class> class TProcessor = FindDefaultProcessor<TResult>::type>
void func()
{
TProcessor<typename TResult::DataType>::func();
}
int main()
{
func<ChildMessage<DataType2>, Processor1>();
return 0;
}
So the question is pretty "simple", how can I make this code compile? in FindDefaultProcessor
, using type = Processor1;
does not compile. I need a mechanism to retrieve a type that is still "open".