mardi 24 avril 2018

How can I specialize a variadic template with a tuple of arbitrary length?

I have the following code that should enable the use of a tuple containing shared pointers to arbitrary types:

#include <memory>
#include <tuple>

template<typename... Types>
using SharedPtrTuple = std::tuple<std::shared_ptr<Types>...>;

template<typename... Components>
class Processor;

template<typename... Components>
class Processor<SharedPtrTuple<Components...>>
{
  // usage of unpacking of shared pointers etc.
};

It works when I instantiate the Processor template with a tuple containing a single type:

class MyComponent {};

int main()
{
  Processor<SharedPtrTuple<MyComponent>> processor;
  return 0;
}

However, when I try to instantiate it with a tuple containing two or more types, it does not compile:

class MyComponentA {};
class MyComponentB {};

int main()
{
  Processor<SharedPtrTuple<MyComponentA, MyComponentB>> processor;
  return 0;
}

Output:

Error   C2079   'processor' uses undefined class 'Processor<std::tuple<std::shared_ptr<MyComponentA>,std::shared_ptr<MyComponentB>>>'

How can I make the compiler find and use my specialization for shared pointer tuples for multiple types? I am using Visual Studio 2015 (VC12) that should have full C++11 support.

Aucun commentaire:

Enregistrer un commentaire