带有auto特性的c++ 17模板参数是否允许约束std::函数对象?
Will C++17 template arguments with auto feature allow constrained std::function objects?
随着即将到来的c++ 17的非类型模板参数auto的特性,是否有可能实现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
对象约束的函数类型,他们接受?
(目前,在GCC上我们得到一个error: 'auto' parameter not permitted in this context
)
这些不是非类型模板参数,因此在c++ 17中不允许使用auto
。
非类型模板实参是指向指针、整数或类似的实际值的模板的实参,而不是类型。
例如
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中以这种方式增强了,但是这样做的语言特性被添加了。
相关文章:
- 函数作为模板参数,是否对返回类型强制约束
- 约束和显式模板实例化
- IpOpt拒绝解决不受约束的问题
- 使用C++模板时表达约束
- 如何在 SCIP C++ 接口中获取 MILP 约束矩阵中的系数值
- 受约束的成员函数和显式模板实例化
- 具有多种约束(例如重量、体积等)的背包
- Gecode 与 Z3 用于约束随机化
- 如果原型是本地的,则使用流 I/O C++类型约束将失败
- C++打开具有 2 个约束的文件
- 如何拥有受约束的运算符模板?
- 不生成单独约束的变量的 Gurobi 影子价格
- 在 C++20 中对概念约束函数进行排序的规则是什么?
- 如何使用 g2o 优化多约束函数
- 约束类模板函数以接受特定的 POD 类型
- 约束包容是否仅适用于概念?
- 检查特定txt文件中的项目是否符合"名称编号USACO"中的约束
- 对类模板成员的显式专用化的约束
- 如何在没有外部库的情况下使用C++03约束执行基于正则表达式的字符串操作
- TS 概念类型名约束