c++函数模板专业化
c++ function template specialization
我正在尝试编写一个函数,该函数接受可变数量的变量类型的参数。类型将始终为POD,但有一种情况除外,即存在POD列表。我已经通过参数包扩展和递归模板函数调用实现了这一点。
该函数目前正在使用模板专用化来覆盖POD的所有覆盖,但我想为List创建一个模板专用化,但要保持列表的模板化。我理解这可能是不可能的,因为这实际上是功能级别的部分专业化。我想知道这种情况的解决办法。
这是我正在做的。
template<typename T, typename ...TArgs>
void Params2(T value, TArgs... args) {
Params2(value);
Params2(args...);
}
template<typename T>
inline void Params2(T c) { //invalid msg }
template <> inline void Params2<unsigned char>(unsigned char c) { //do something with char }
template <> inline void Params2<char>(char c) { //do something with uchar }
template <> inline void Params2<unsigned short>(unsigned short c) { //do something with short }
template <> inline void Params2<short>(short c) { //do something with ushort }
// more POD overrides
如果我做以下操作,它会起作用,因为我已经完全定义了列表的类型:
template <>
inline void Params2< libfc::List<int> >(libfc::List<int> l)
{
for (auto itt = l.Begin(); itt != l.End(); ++itt)
{
//do something with each int
}
}
我想做的是:
template <>
template <typename R>
inline void Params2<libfc::List<R> >(libfc::List<R>) {
//do something with for each element of type R
}
我不想为函数可以采用的每种类型的List进行重写,但如果没有更好的选项,我可以接受。
edit:调用应用程序将按照以下行调用此功能(不是有效代码,只是示例):
Params2(10, 25, "test", List<int> { 5, 10 }, List<double> { 3.14, 9.81 } );
使用重载而不是专用
inline void Params2(unsigned char c) { //do something with char }
inline void Params2(char c) { //do something with uchar }
inline void Params2(unsigned short c) { //do something with short }
inline void Params2(short c) { //do something with ushort }
template<typename T>
void Params2(const std::list<T>& value) {
// list
}
template<typename T, typename ...TArgs>
void Params2(T value, TArgs... args) {
Params2(value);
Params2(args...);
}
或者使用通用方法和类专用化
template<typename T>
struct helper;
template<>
struct helper<unsigned char> { void operator () (unsigned char c) { /**/}};
template<>
struct helper<char> { void operator () (char c) { /**/}};
template<typename T>
struct helper<std::list<T>> { void operator () (const std::list<T>& l) { /**/}};
template<typename T>
void Params2(const T& t) {
helper<T>{}(t);
}
使用std::enable_if()
,您可以制作混合了原始数据和原始数据"列表"的模板。这里的技巧是,任何具有const_iterator
类型和返回可转换为该类型的类型的begin()
方法的内容都将被视为列表,其他内容都将路由到适当的重载。
void foo(const int& t) { std::cout << "int: " << t << std::endl; }
void foo(const double& t) { std::cout << "double: " << t << std::endl; }
template<typename T,
typename std::enable_if<
std::is_convertible<
decltype(std::declval<const T&>().begin()),
typename T::const_iterator
>::value
>::type* = nullptr>
void foo(const T& t) {
auto begin = t.begin();
for(;begin != t.end(); ++begin) {
foo(*begin);
}
}
template<typename T,
typename... TArgs>
void foo(const T& first, TArgs... args) {
foo(first);
foo(args...);
}
int main() {
std::list<int> asdf{3, 4, 5};
std::vector<double> vec{7, 8, 9};
foo(1, 2.0, asdf, 6, vec);
}
输出:
int:1
双人间:2
int:3
int:4
int:5
int:6
双人间:7
双人间:8
双:9
相关文章:
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 将重载的成员函数传递给函数模板
- C++17中函数模板中的静态数组初始化(MSVC 2019)
- 为什么 gcc 和 clang 为函数模板的实例化生成不同的符号名称?
- 具有常量引用参数的函数模板专用化
- std::span<const T> 作为函数模板中的参数
- 函数模板部分专业化-有什么解决方法吗
- 我应该声明我的函数模板专业化还是定义它们就足够了
- 为什么这不是函数模板的部分专业化
- 在允许的函数模板的明确专业化中扣除了多个模板参数
- C - 具有非类型模板参数的模板类上的专业化函数模板
- C++11性能:Lambda内联与函数模板专业化
- 成员函数模板参数的部分专业化
- 实例化错误后成员函数模板的专业化,以及成员函数的顺序
- 函数模板专业化语法
- 为什么类中不允许函数模板专业化
- 函数模板在另一个类/命名空间中的专业化
- c++函数模板专业化
- c++函数模板专业化
- 函数模板专业化和Abrahams/Dimov示例