元结构与元函数
Metastructures vs metafunctions
为什么我们仍然使用结构和typedef
s(或using
s)进行元编程?
看看这个问题中的代码——推断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
的函数重载更好,或者这只是程序员惰性的一种情况?
我能想到使用函数重载的几个问题:
不同的匹配规则;模板专业化列表只会完全匹配,而如果参数可以转换为参数类型,则函数重载会匹配。这通常可以解决,但在某些情况下可能会导致更复杂的元代码。
退货类型限制;函数不能返回某些类型,例如函数(而不是函数指针)、抽象类、不可压缩类型(我认为)、未知绑定的数组(可能)。这可以通过将类型封装在模板结构中来解决。
一般来说,您可能会看到一方面使用typename
和using
,另一方面使用decltype
和包装模板之间的折衷。
不过,我同意您的上述代码是对原始代码的改进,因为仅消除了bool
技巧。
相关文章:
- 使用不带参数的函数访问结构元素
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 链表,反向函数,数据结构
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 需要帮助将结构数组传递给函数
- 使用cpp中的结构和函数的多个学生条目
- 如何传递带有通过引用传递的结构参数的函数?
- 在类构造函数中使用结构变量
- 在 c++ 中将变量作为结构构造函数中的引用传递
- 在类构造函数中定义结构变量的参数
- 如何在模板类函数中分配结构值?
- 我应该在C++中将这些结构用作参数化构造函数吗?
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 在结构外部调用指向函数的指针
- 函数:结构的数组
- C++错误:只能为对象和函数结构指定存储类
- 将libuv与函数结构一起使用,而不是函数回调
- 从c#调用c++函数-结构体,指针,函数指针
- 函数结构数组输入cin问题