考虑以下代码:
struct S {
...
template<typename T>
T operator() {
...
}
};
只能使用以下方式调用它:
s.operator()<Foo>();
有没有一种方法可以将类型名作为参数传递并因此调用如下函数:
s(Foo);
相反?
答案 0 :(得分:3)
将类型存储在模板标记类型struct中:
template<class T> struct tag_t { };
template<class T> tag_t<T> tag{};
struct S {
template<class T>
T operator()( tag_t<T> );
} s;
s( tag<Foo> );
答案 1 :(得分:1)
怎么样:
struct S {
template<class T>
void operator()(const T&) { }
} s;
struct Foo {};
Foo foo;
s (foo);
距离您想要的距离够近吗?