Dav*_*ian 8 c++ templates auto std-function c++17
随着即将推出的带有auto的非类型模板参数的 C++ 17特性,是否可以以std::function能够放置例如以下函数的方式实现:
    bool f(int n,  double d) {}    
    bool g(bool b, char c)   {}
    bool h(bool b)           {}
进入自动模板化的std::function对象:
   std::function<bool(auto,   auto)> faa = f; // ok
   std::function<bool(int,    auto)> fia = f; // ok
   std::function<bool(double, auto)> fda = f; // error: function type mismatch
   std::function<bool(auto,   auto)> gaa = g; // ok
   std::function<bool(auto,   auto)> haa = h; // error: function type mismatch
   std::function<bool(auto)>         ha  = h; // ok
等等.
换句话说,让std::function对象受限于他们接受的函数类型?
(目前,在海湾合作委员会,我们得到了error: 'auto' parameter not permitted in this context.)
这些不是非类型模板参数,因此auto在C++ 17中不允许这样做.
非类型模板参数是模板的参数,这些模板是指针或整数或类似的,实际值,而不是类型.
举个例子,
std::integral_constant<std::size_t, 7>;
这里7是类型std::size_t和值的非类型模板参数7.
非类型模板auto允许以下内容:
template<auto x>
using integral = std::integral_constant< decltype(x), x >;
现在integral<7>是std::integral_constant<int, 7>.
另一方面,您的使用auto取代了类型,而不是非类型.
有一个功能可以推导出模板的类型,因此您可以编写:
std::function faa = f;
如果它们被扩充std::function为能够从函数指针(或非模板可调用)推导出签名.
但请注意,这std::function将具有固定签名,而不是模板签名.该功能只允许演绎,而不是模板动态调度.
我不知道std::function在C++ 17中是否以这种方式进行了扩充,但添加了这样做的语言功能.
| 归档时间: | 
 | 
| 查看次数: | 1321 次 | 
| 最近记录: |