我有一个带有模板构造函数的类,如下所示:
class Foo {
private:
std::unordered_map<std::type_index, std::vector<std::function<void(BaseT*)>>> funcs;
public:
template<class T> Foo(const std::function<void(T* arg)>& func) {
auto funcToStore = [func](BaseT* a) { func(static_cast<T*>(a)); };
this->funcs[std::type_index(typeid(T))].push_back(funcToStore);
}
}
此类的构造函数使用带有从某些基本类型
T
派生的BaseT
类型的参数的函数参数,并将该函数存储在利用std::type_info
的T
作为键的 vector 映射中。由于这是模板构造函数,而不是普通函数,因此明确指定template参数无效,因为这是不允许的语法:
Foo* foo = new Foo<MyT>([](MyT* arg) { ... });
省略显式的
<MyT>
也不起作用,因为无法从lambda参数类型推导出template参数。因此,一种解决方案是将lambda包装在
std::function
对象中:Foo* foo = new Foo(std::function<void(MyT*)>([](MyT* arg) { ... }));
但这显然不是一个很好的可读语法。
到目前为止,我提出的最好的办法是为
std::function
使用别名:template<class T> using Func = std::function<void(T*)>;
Foo* foo = new Foo(Func<MyT>([](MyT* arg) { ... }));
这是更短的代码,在lambda参数中使用
auto
关键字时,我只需要指定一次实际的MyT
类型,因此最后似乎是一个不错的解决方案。但是,还有其他甚至更短的解决方案吗?这样就不必包装lambda了?喜欢:
Foo* foo = new Foo([](MyT* arg) { ... });
最佳答案
使用普通模板参数代替std::function
:
class Foo {
std::unordered_map<size_t, std::vector<BaseT*>> funcs;
public:
template<class T> Foo(const T& func) {
// ...
}
};
现在推导将正确进行,并且您的代码将不会受到
std::function
的开销的困扰。如果要获取lambda的第一个参数的类型怎么办?
您必须做这样的事情:
template<typename T>
struct function_traits : function_traits<&T::operator()> {};
template<typename R, typename C, typename... Args>
struct function_traits<R(C::*)(Args...) const> {
using arguments = std::tuple<Args...>;
using result = R;
};
当然,如果要支持函数类型的所有可能情况,则需要32 specialisations
现在,如果需要,您可以提取参数类型,甚至提取类型:
template<class T> Foo(const T& func) {
using Arg = std::tuple_element_t<0, typename function_traits<T>::arguments>;
auto funcToStore = [func](BaseT* a) { func(static_cast<Arg>(a)); };
funcs[typeid(Arg).hash_code()].push_back(funcToStore);
}
另外,由于您在构造函数中收到了
const T&
,因此您可能希望将函数限制为只能使用可以编译的函数进行调用:template<typename T>
using is_valid_foo_function = std::is_convertible<
BaseT*, // form
std::tuple_element_t<0, typename function_traits<T>::arguments> // to
>;
并使用这样的约束:
template<class T, std::enable_if_t<is_valid_foo_function<T>::value>* = nullptr>
Foo(const T& func) {
// ...
}