【问题标题】:Template class partial specialization for kinds of collections各种集合的模板类部分特化
【发布时间】:2016-03-12 15:36:49
【问题描述】:

我正在尝试编写 2 个模板部分特化,它们应该以不同方式处理序列容器和关联容器的序列化,但似乎根本没有使用序列容器特化,因为编译器试图用错误的特化对其进行特化,代码如下:

template<typename W, typename T, template<typename...> class C, typename... Args>
class Archiver<W, C<T, Args...>>
{
public:
  template<typename U = C<T, Args...>, typename std::enable_if<std::is_same<typename U::value_type, T>::value, int>::type = 0>
  static void serialize(const W& w, const C<T, Args...>& container)
  {
    ...
  }

  template<typename U = T, typename std::enable_if<!std::is_base_of<archive::require_placement_move, U>::value, int>::type = 0,
  typename J = C<T,Args...>, typename std::enable_if<std::is_same<typename J::value_type, T>::value, int>::type = 0>
  static void unserialize(const W& r, const Context& c, C<T,Args...>& container)
  {
    ...
  }

  template<typename U = T, typename std::enable_if<std::is_base_of<archive::require_placement_move, U>::value, int>::type = 0,
  typename J = C<T,Args...>, typename std::enable_if<std::is_same<typename J::value_type, T>::value, int>::type = 0>
  static void unserialize(const W& r, const Context& c, C<T,Args...>& container)
  {
    ...
  }
};

template< typename W, typename K, typename T, template<typename...> class M, typename... Args>
class Archiver<W, M<K, T, Args...>>
{
public:
  template<class U = M<K,T,Args...>, typename std::enable_if<std::is_same<typename U::key_type, K>::value && std::is_same<typename U::mapped_type, T>::value, int>::type = 0>
  static void serialize(const W& w, const M<K,T,Args...>& container)
  {
    ...
  }

  template<class U = M<K,T,Args...>, typename std::enable_if<std::is_same<typename U::key_type, K>::value && std::is_same<typename U::mapped_type, T>::value, int>::type = 0>
  static void unserialize(const W& r, const Context& c, M<K,T,Args...>& container)
  {
    ...
  }
};

如果我尝试使用

Writer w;
Archiver<Writer, std::list<float>>::serialize(...);

解决方案是在第二个专业化上完成的,因此导致

Candidate template ignored: substitution failure [with U = std::__1::list&lt;float, std::__1::allocator&lt;float&gt; &gt;]: no type named 'key_type' in 'std::__1::list&lt;float, std::__1::allocator&lt;float&gt; &gt;'

这应该意味着先验排除了第一个专业化,例如推断解决方案是选择与地图相关的专业化。或者也许我错过了一些非常愚蠢的东西,因为我一直在处理这段代码。

【问题讨论】:

  • 我认为这是因为偏序

标签: c++ templates c++11 template-specialization


【解决方案1】:

我认为你对 SFINAE 的使用有点过时了。

您在Archive 模板中明确指定容器类型。一些未知的 C++ 规则意味着当您通过 std::list&lt;float&gt; 时选择了第二个特化。 SFINAE 然后应用于serialize 函数,消除了only 可用的重载(因此出现错误消息)。因为已经选择了Archive 的第二个特化,所以第一个特化中的serialize 函数从未考虑过。这不是你想要的。

由于您明确指定容器类型,我不确定您为什么要尝试使用 SFINAE。要正确地分别专门针对序列容器和关联容器,您可能必须等到概念标准化。在那之前,你可以做一些天真的事情,比如根据类型别名 key_type 的存在进行专门化(这就是你已经在做的事情)。

这是一个例子:

#include <type_traits>

template<typename>
using void_t = void;

template<typename T, typename = void>
struct has_key_type : std::false_type
{
};

template<typename T>
struct has_key_type<T, void_t<typename T::key_type>> : std::true_type
{
};

template<typename C, typename = std::enable_if_t<has_key_type<C>::value>>
void serialize(C const& c)
{
};

template<typename C, typename = std::enable_if_t<!has_key_type<C>::value>, typename = void>
void serialize(C const& c)
{
};

#include <iostream>
#include <list>
#include <map>

int main()
{
  serialize(std::list<int>());
  serialize(std::map<float, float>());
}

【讨论】:

    猜你喜欢
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多