c++templatestemplate-specializationpartial-specializationexplicit-specialization

Template template partial specialization


Consider the following class template.

template<template<typename U> typename T>
class Entity {
  // Something
};

What is the proper syntax to specialize Entity explicitly:

I cannot find out the proper syntax for this task. The cppreference page does not provide any examples on this issue.


Solution

  • Here an example of specialization:

    template <template<typename> typename C>
    class Entity {
      // Something
    };
    
    template <typename>
    struct A{};
    
    template <>
    class Entity<A>
    {
    // ...
    };
    // Usage is Entity<A> entity;
    

    Demo

    But you probably want something like:

    template <typename T>
    class Entity {
      // Something
    };
    
    template <typename>
    struct A{};
    
    template <typename T>
    class Entity<A<T>>
    {
        // ...
    };
    
    template <typename T, template <typename> class C>
    class Entity<C<T>>
    {
        // ...
    };
    
    // Usage is Entity<A<int>> entity;
    

    Demo