Search code examples
c++variadic-templatestemplate-meta-programming

Using template programming to change the type of the container


I have been reading through a template metaprogramming tutorial and I am struck at this example from that book.

 /* Change the type container */
  template<class NewList, class List>
  struct rename_container;
  
  template<template<class...> class NewList,
           template<class...> class List,
           class... Elements>
  struct rename_container<NewList, List<Elements...>>
  {
      using new_list = NewList<Elements...>;
  };

  int main()
  {
      rename_container<std::variant, std::tuple<int, float, double>> v;
      return 0;
  }

I am getting the following error among other errors.

type_containers.cpp:52:50: error: type/value mismatch at argument 1 in template parameter list for ‘template<class NewList, class List> struct rename_container’
 struct rename_container<NewList, List<Elements...>>
                                                  ^~
type_containers.cpp:52:50: note:   expected a type, got ‘NewList’

Can someone help me troubleshoot this?


Solution

  • Seems there is a typo, it should be

    /* Change the type container */
      template<template<class...> class NewList, class List>
      struct rename_container;
    

    and probably:

    rename_container<std::variant, std::tuple<int, float, double>>::new_list v;
    // v is std::variant<int, float, double>