我有一个要求,即我将可变参数模板参数传递给函数。当然,根据模板参数,将会有不同的函数实现。我需要将操纵器识别为模板参数。
//If T is manipulator call this method
template <typename T, typename... Args>
void foo(const T& t, const Args&... args){
/*Do something if T is manipulator*/
}
//Otherwise this one
template <typename T, typename... Args>
void foo(const T& t, const Args&... args){
/*Do something if T is not a manipulator type*/
}
我尝试过使用std::enable_if<std::is_same<decltype(/*some specific manipulator*/), T>::value>
。但这种方法的一个问题是一些操纵者不适合这种情况。有人可能会建议我提供比上述更好的解决方案。此外,并非所有操纵器都具有相同的签名,并且它们的返回类型是在编译时定义的。
Dynamically changing dropdowns in IPython notebook widgets and Spyre
答案 0 :(得分:1)
我仍然不确定你要做什么,但如果你只是想看看你是否可以将它应用到流中,那么这可能会做你想要的吗?
#include <iostream>
#include <ios>
#include <type_traits>
using namespace std;
template<class T>
using int_t = int;
template<typename T, int_t<decltype(operator<<(std::declval<istream>(), declval<T>()))> = 0>
void go(T f){
cout << f << true << false << endl;
}
struct foo{};
int main()
{
go(&boolalpha);
go(foo());
}