如何调用模板类型

AAA*_*AAA 3 c++ templates c++11

我正在尝试创建一个模板函数,它具有两个基于模板参数的不同实现.第一个是针对I/O操纵器的,第二个针对任何一般情况.

template <typename T>
typename std::enable_if<
            std::is_same<decltype(std::setw), T>::value>::type
foo(T& t){
        cout << "enabled" << std::endl;
}

template <typename T>
void foo(T& t){
        cout << "Normal" << std::endl;
}

template <typename... Ts>
void foohelper(Ts... t){
        foo(t...);
}
int main(){
        foohelper(std::setprecision(3)); // It should print enabled, but printing Normal
}
Run Code Online (Sandbox Code Playgroud)

目前它没有做我想要实现的.我该如何解决?

Ton*_*roy 5

您不需要使用enable_if- 只需专门化主要定义下面的模板template foo.

template <>
void foo<decltype(std::setw(0))>(decltype(std::setw(0))& t)
{
        cout << "enabled" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)