admin管理员组

文章数量:1124680

Consider the following class template.

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

What is the proper syntax to specialize Entity explicitly:

  • by U,
  • by T (keeping it as a template)?

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

Consider the following class template.

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

What is the proper syntax to specialize Entity explicitly:

  • by U,
  • by T (keeping it as a template)?

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

Share Improve this question edited 2 days ago Kaiyakha asked 2 days ago KaiyakhaKaiyakha 1,8131 gold badge11 silver badges28 bronze badges 8
  • 1 "by U" there are not really U there. – Jarod42 Commented 2 days ago
  • There is no syntax to specialize by U, since a "fixed U" changes the argument from a template into a type. The parmeter is T only, and an argument for it would be something like std::vector (no args), whereas std::vector<int> won't be a proper argument since it's a type, not a template. – StoryTeller - Unslander Monica Commented 2 days ago
  • "keeping it as a template" Not sure what you mean here. – Jarod42 Commented 2 days ago
  • @Jarod42 it means the T parameter must be a concrete template, not just some T template. – Kaiyakha Commented 2 days ago
  • @Jarod42 what do you mean by there are not really U there? – Kaiyakha Commented 2 days ago
 |  Show 3 more comments

1 Answer 1

Reset to default 2

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

本文标签: cTemplate template partial specializationStack Overflow