iou*_*vxz 1 c++ grammar traits template-meta-programming
基本上,我想让一个函数对于向量(类型)参数和非向量类型参数表现不同。
#include <vector>
using namespace std;
template <typename type>
struct is_vector {
static const bool value = false;
};
template <typename type>
struct is_vector<vector<type>>
{
static const bool value = true;
};
template <typename type>
type read()
{
if (is_vector<type>::value)
{
type vec(10);
vec.front()=1;//left of '.front' must have class/struct/union
return vec;
}
else
{
return{};
}
}
int main()
{
auto i= read<int>();
}
Run Code Online (Sandbox Code Playgroud)
我想在使用 vector 作为类型名时返回一个向量,在使用 int 作为类型名时返回一个 int 。
但既然 is_vector(int)::value 返回 false ,为什么我的编译器会报告“‘.front’的左边必须有类/结构/联合”?我怎样才能让它工作?
我想要实现的是将字符串正确反序列化为向量(类型)或向量(向量(类型))。
我需要递归调用 read 函数,同时传递一个多重向量作为模板参数,但编译器禁止我这样做。
template <typename type>
struct is_vector {
static const bool value = false;
};
template <typename type>
struct is_vector<vector<type>>
{
static const bool value = true;
};
template <typename type>
type read(char*& str)
{
if (is_vector<type>::value)
{
type vec(read<uint8_t>(str));
for (auto& e : vec)
e = read<type::value_type>(str);
return vec;
}
return *reinterpret_cast<type*>((str += sizeof(type)) - sizeof(type));
}
Run Code Online (Sandbox Code Playgroud)
所以我尝试了专业化。
template<>
vector<int> read<vector<int>>(char*& str)
{
vector<int> vec(read<uint8_t>(str));
for (auto& e : vec)
e = read<int>(str);
return vec;
}//works
template <typename type>
template <>
vector<type> read<vector<type>>(char*& str)
{
vector<type> vec(read<uint8_t>(str));
for (auto& e : vec)
e = read<type>(str);
return vec;
}//don't work
Run Code Online (Sandbox Code Playgroud)
我真的需要为我使用的每种类型手动重写我的读取函数吗?
(比如向量(向量(向量(int)))?)
您需要一个foo<R>
至少由返回类型参数化的函数模板R
,并且您需要一个针对任意类型的专门实现 when R
= 。std::vector<U>
U
论点是什么并不重要foo<R>
,因此为了便于说明,我们假设没有任何论点。操作方法如下:
定义特征模板如下:
template<typename T>
struct is_vector
{
static constexpr bool value = false;
};
template<template<typename...> class C, typename U>
struct is_vector<C<U>>
{
static constexpr bool value =
std::is_same<C<U>,std::vector<U>>::value;
};
Run Code Online (Sandbox Code Playgroud)
有了这个,
is_vector<T>::value
Run Code Online (Sandbox Code Playgroud)
T
当且仅当=时std::vector<U>
,对于某些 ,在编译时才为真U
。
foo<R>()
然后在以下几行定义两个重载:
template <typename R>
std::enable_if_t<!is_vector<R>::value,R> foo()
{
// Your non-vector implementation instead of...
std::cout <<
"In non-vector specialization of `foo<R>()`\n";
return R();
}
template <typename R>
std::enable_if_t<is_vector<R>::value,R> foo()
{
// Your vector implementation instead of...
std::cout <<
"In vector specialization of `foo<R>()`\n";
return R();
}
Run Code Online (Sandbox Code Playgroud)
这两个重载是互斥的并且共同详尽的。当且仅当为 false 时,第一个重载才是合法代码is_vector<R>::value
。is_vector<R>::value
当且仅当为 true时,第二个重载才是合法代码。这要归功于 的行为std::enable_if
,您应该研究和理解它。
当编译器需要选择这些模板重载之一来实现它在代码中找到的某些调用时,它会发现在为模板参数插入时foo<type>()
,其中一个重载甚至无法编译。第一个如果is some 则不会编译
,第二个如果is not some
则不会编译。有用的是,编译器选择它可以编译的那个。这就是所谓的(“替换失败不是错误”)type
R
type
std::vector<U>
type
std::vector<U>
SFINAE
,它是您问题的解决方案。
这是一个说明性程序:
#include <vector>
#include <type_traits>
#include <iostream>
template<typename T>
struct is_vector
{
static constexpr bool value = false;
};
template<template<typename...> class C, typename U>
struct is_vector<C<U>>
{
static constexpr bool value =
std::is_same<C<U>,std::vector<U>>::value;
};
template <typename R>
std::enable_if_t<!is_vector<R>::value,R> foo()
{
// Your non-vector implementation instead of...
std::cout <<
"In non-vector specialization of `foo<R>()`\n";
return R();
}
template <typename R>
std::enable_if_t<is_vector<R>::value,R> foo()
{
// Your vector implementation instead of...
std::cout <<
"In vector specialization of `foo<R>()`\n";
return R();
}
int main()
{
auto i = foo<int>();
(void)i;
auto vc = foo<std::vector<char>>();
(void)vc;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这将输出:
In non-vector specialization of `foo<R>()`
In vector specialization of `foo<R>()`
Run Code Online (Sandbox Code Playgroud)
(gcc 6.1/clang 3.8,-std=c++14
参见实时)