元结构与元函数

Metastructures vs metafunctions

本文关键字:函数 结构      更新时间:2023-10-16

为什么我们仍然使用结构和typedefs(或usings)进行元编程?

看看这个问题中的代码——推断lambda或任意可调用的"的调用签名;make_function":

template<typename T> struct remove_class { };
template<typename C, typename R, typename... A>
struct remove_class<R(C::*)(A...)> { using type = R(A...); };
template<typename T, bool> struct get_signature_impl { };
template<typename R, typename... A>
struct get_signature_impl<R(A...), true> { using type = R(A...); };
template<typename R, typename... A>
struct get_signature_impl<R(*)(A...), true> { using type = R(A...); };
template<typename T>
struct get_signature_impl<T, true> { using type = typename remove_class<
    decltype(&std::remove_reference<T>::type::operator())>::type; };

有很多奇怪的技巧,比如bool,像typename这样嘈杂的关键词,像struct get_signature_impl;这样多余的东西
我们在C++11中获得了using关键字,这很好,但没有太大区别。

在C++11中,我们有decltype和尾随返回类型。有了这种能力,我们可以丢弃所有丑陋的元结构,并编写美丽的元函数
因此,我们可以重写上面的代码:

template<typename C, typename R, typename... A> auto make_function_aux(R(C::*)(A...)) -> std::function<R(A...)>;
template<typename C, typename R, typename... A> auto make_function_aux(R(C::*)(A...) const) -> std::function<R(A...)>;
template<typename R, typename... A> auto make_function_aux(R(A...)) -> std::function<R(A...)>;
template<typename R, typename... A> auto make_function_aux(R(*)(A...)) -> std::function<R(A...)>;
template<typename T> auto make_function_aux(const T&) -> decltype(make_function_aux(&T::operator()));
template<typename F> auto make_function(F&& f) -> decltype(make_function_aux(f)) { return decltype(make_function_aux(f))(std::forward<F>(f)); }

在匹配模板参数时,模板部分专业化是否比decltype的函数重载更好,或者这只是程序员惰性的一种情况?

我能想到使用函数重载的几个问题:

不同的匹配规则;模板专业化列表只会完全匹配,而如果参数可以转换为参数类型,则函数重载会匹配。这通常可以解决,但在某些情况下可能会导致更复杂的元代码。

退货类型限制;函数不能返回某些类型,例如函数(而不是函数指针)、抽象类、不可压缩类型(我认为)、未知绑定的数组(可能)。这可以通过将类型封装在模板结构中来解决。

一般来说,您可能会看到一方面使用typenameusing,另一方面使用decltype和包装模板之间的折衷。

不过,我同意您的上述代码是对原始代码的改进,因为仅消除了bool技巧。