内部模板类型 std::vector<std::vector<T>> 的函数模板重载或特化
如何实现内部模板类型std::vector<:vector>>
的模板重载功能.
How to achieve function Template Overloading for inner template type std::vector<std::vector<T>>
.
我有一个重载模板程序和一个包含映射、对和向量的复杂数据结构.
I have a program of overloaded templates and a complex data structure having maps, pairs and vectors.
#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<std::vector<T>>
我需要调用 #5 并且我收到编译错误 wrtstd::enable_if 中使用的 ::type.请让我知道这个程序有什么问题以及如何使它工作.也有人可以解释一下#6 和#7 表示 w.r.t 模板参数包是什么,它是如何工作的.
Always for vectors #2 is getting called, but for std::vectors<std::vector<T>>
I need #5 to get called and also I am getting compilation error w.r.t ::type used in std::enable_if.
Please let me know what is wrong in this program and how to make it work.
Also Can some one explain what does #6 and #7 signify w.r.t template parameter pack, how does it work.
谢谢.
我看到的为 std::vector<std::vector<T>>
专门化编写的最简单方法>f() 如下
The simplest way I see to write a std::vector<std::vector<T>>
specialization for f()
is the following
template<typename T>
bool f (std::vector<std::vector<T>>& v) // #5
{
std::cout << "body of f for vectors<vectors>\n";
return true;
}
这样你就重载了比 #2
更专业的模板 f()
函数.
This way you have overloaded template f()
function that is more specialized than #2
.
如果您想将 SFINAE 与您的 is_specialization
一起使用,在我看来正确的方法如下
If you want to use SFINAE with your is_specialization
, seems to me that the right way is the following
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<:vector> 调用
,你会得到一个歧义,所以编译错误.f()
时;>
Unfortunately this version is specialized as version #2
, so when you call f()
with a std::vector<std::vector<T>>
, you get an ambiguity so a compilation error.
要解决此问题,您还必须禁用#2
版本
To solve this problem you have also to disable the #2
version
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
已定义.
In your original version... you use typename T::type
... but this gives an error when T
isn't a class with a type
defined.
更多:返回两种类型
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
Using SFINAE this way, the returned type has to be expressed by std::enable_if