【问题标题】:Function Template Overloading or Specialization for inner template type std::vector<std::vector<T>>内部模板类型 std::vector<std::vector<T>> 的函数模板重载或特化
【发布时间】:2019-12-24 06:35:38
【问题描述】:

如何实现内部模板类型std::vector&lt;std::vector&lt;T&gt;&gt;的模板重载功能。

我有一个重载模板程序和一个包含映射、对和向量的复杂数据结构。

#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <typeinfo>

template<typename Test, template<typename...> class Ref> //#6
struct is_specialization : std::false_type {};

template<template<typename...> class Ref, typename... Args> //#7
struct is_specialization<Ref<Args...>, Ref>: std::true_type {};
template <typename T>
bool f(T& x) // #1
{
    std::cout << "body of f\n";
    return f(x);
}

template <typename T>
bool f(std::vector<T>& v) // #2
{
    std::cout << "body of f for vectors\n";
    return true;
}
template<typename T>
typename std::enable_if<is_specialization<typename T::value, std::vector>::value, T>::type
    bool f(std::vector<T>& v) // #5
    {
        std::cout << "body of f for vectors<vectors>\n";
        return true;
    }

template <typename Key, typename Value>
bool f(const std::pair<Key,Value>& v) // #3
{
    std::cout << "body of f for pairs\n";
    for(auto& e: v) {
      f(e.first);
    }
    for(auto& e: v) {
      f(e.second);
    }
    return true;
}

template <typename Key, typename Value>
bool f(std::map<Key,Value>& v) // #4
{
    std::cout << "body of f for maps\n";
    for(auto& e: v) {
      f(e.first);  // expecting this call goes to #3
    }
    for(auto& e: v) {
      f(e.second);
    }
    return true;
}

int main() {
  std::vector<int> v{1,2};
  std::map<std::pair<int,int>,std::vector<std::vector<int>>> m_map = {
                                            {{10,20}, {{5,6},{5,6,7}}},
                                            {{11,22}, {{7,8},{7,8,9}}}
                                        };
    f(m_map); // this call goes to #4
} 

总是调用向量#2,但对于std::vectors&lt;std::vector&lt;T&gt;&gt;,我需要调用#5,并且我得到编译错误w.r.t ::type 在std::enable_if 中使用。 请让我知道这个程序有什么问题以及如何使它工作。 也有人能解释一下 #6 和 #7 表示 w.r.t 模板参数包是什么,它是如何工作的。

谢谢。

【问题讨论】:

  • 你试过为std::vector&lt;std::vector&lt;T&gt;&gt;写一个重载吗? (您的#6/#7 问题应该是一个单独的问题。)
  • @Davis 是的,我没有成功,是的 #6 和 #7 我可以将其作为一个单独的问题

标签: c++ templates overloading stdvector template-specialization


【解决方案1】:

我看到为f() 编写std::vector&lt;std::vector&lt;T&gt;&gt; 专业化的最简单方法如下

template<typename T>
bool f (std::vector<std::vector<T>>& v) // #5
 {
   std::cout << "body of f for vectors<vectors>\n";
   return true;
 }

这样你就重载了模板f() 函数,它比#2 更专业。

如果您想将 SFINAE 与 is_specialization 一起使用,在我看来正确的方法如下

template <typename T>
typename std::enable_if<is_specialization<T, std::vector>::value, bool>::type
f (std::vector<T> & v) // #5
 {
   std::cout << "body of f for vectors<vectors>\n";
   return true;
 }

不幸的是,这个版本被特化为版本#2,所以当你用std::vector&lt;std::vector&lt;T&gt;&gt; 调用f() 时,你会得到一个歧义,所以会出现编译错误。

要解决此问题,您还必须禁用 #2 版本

template <typename T>
typename std::enable_if<! is_specialization<T, std::vector>::value, bool>::type
f (std::vector<T> & v) // #2
 {
   std::cout << "body of f for vectors\n";
   return true;
 }

在您的原始版本中...您使用typename T::type...但是当T 不是定义了type 的类时,这会出错。

更多:你返回两种类型

template<typename T>
typename std::enable_if<is_specialization<typename T::value,
                        std::vector>::value, T>::type // <<--- type 1: T
    bool f(std::vector<T>& v) // #5
//  ^^^^  type2: bool

这样使用SFINAE,返回的类型必须用std::enable_if表示

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    相关资源
    最近更新 更多