c++函数模板专业化

c++ function template specialization

本文关键字:专业化 函数模板 c++      更新时间:2023-10-16

我正在尝试编写一个函数,该函数接受可变数量的变量类型的参数。类型将始终为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