当前位置: 首页 > news >正文

网站 文件夹 上传镇江网友之家

网站 文件夹 上传,镇江网友之家,上海嘉定区网站建设,wordpress批量修改文章信息文章目录 一、引言二、std::is_invocable 概述代码示例输出结果 三、std::is_invocable 的工作原理简化实现示例 四、std::is_invocable 的相关变体1. std::is_invocable_r2. std::is_nothrow_invocable 和 std::is_nothrow_invocable_r 五、使用场景1. 模板元编程2. 泛型算法 … 文章目录 一、引言二、std::is_invocable 概述代码示例输出结果 三、std::is_invocable 的工作原理简化实现示例 四、std::is_invocable 的相关变体1. std::is_invocable_r2. std::is_nothrow_invocable 和 std::is_nothrow_invocable_r 五、使用场景1. 模板元编程2. 泛型算法 六、注意事项七、结论 一、引言 在现代 C 编程中我们经常会编写一些通用的代码这些代码需要处理不同类型的可调用对象如函数、函数指针、成员函数指针、lambda 表达式等。在使用这些可调用对象之前我们可能需要在编译时就确定它们是否可以以特定的参数列表进行调用。C17 引入的 std::is_invocable 系列类型特征就为我们提供了这样的能力它允许我们在编译时进行调用可行性的检查从而增强代码的健壮性和通用性。 二、std::is_invocable 概述 std::is_invocable 是定义在 type_traits 头文件中的一个模板元函数。它用于在编译时检查一个可调用对象是否可以使用给定的参数类型进行调用。std::is_invocable 有多个重载形式基本形式如下 template class F, class... Args struct is_invocable;template class F, class... Args inline constexpr bool is_invocable_v is_invocableF, Args...::value;代码示例 #include iostream #include type_traits// 普通函数 void foo(int x) {std::cout foo called with x std::endl; }int main() {std::cout std::boolalpha;// 检查 foo 是否可以用 int 类型参数调用std::cout Is foo invocable with int? std::is_invocable_vdecltype(foo), int std::endl;// 检查 foo 是否可以用 double 类型参数调用隐式转换可行std::cout Is foo invocable with double? std::is_invocable_vdecltype(foo), double std::endl;return 0; }输出结果 Is foo invocable with int? true Is foo invocable with double? true在上述代码中我们定义了一个普通函数 foo它接受一个 int 类型的参数。然后使用 std::is_invocable_v 检查 foo 是否可以用 int 和 double 类型的参数调用。由于 double 可以隐式转换为 int所以两种检查结果都为 true。 三、std::is_invocable 的工作原理 std::is_invocable 的实现基于 SFINAESubstitution Failure Is Not An Error原则。当我们使用 std::is_invocableF, Args... 时编译器会尝试在编译时构造一个对可调用对象 F 的调用参数类型为 Args...。如果这个调用是合法的那么 std::is_invocableF, Args...::value 将为 true否则它将为 false。 简化实现示例 #include type_traits// 辅助模板用于检测调用是否可行 template typename F, typename... Args, typename void struct is_invocable_helper : std::false_type {};template typename F, typename... Args struct is_invocable_helperF, Args..., std::void_tdecltype(std::declvalF()(std::declvalArgs()...)): std::true_type {};// 定义 is_invocable template typename F, typename... Args struct is_invocable : is_invocable_helperF, Args... {};// 辅助模板用于打印结果 template typename F, typename... Args void print_is_invocable() {std::cout Is callable with given args? is_invocableF, Args...::value std::endl; }// 普通函数 void bar(int x) {}int main() {std::cout std::boolalpha;print_is_invocabledecltype(bar), int();return 0; }在这个示例中我们定义了一个辅助模板 is_invocable_helper它使用 std::void_t 和 decltype 来检测对可调用对象 F 的调用是否合法。如果合法is_invocable_helper 将继承自 std::true_type否则它将继承自 std::false_type。 四、std::is_invocable 的相关变体 1. std::is_invocable_r std::is_invocable_r 用于检查一个可调用对象是否可以使用给定的参数类型进行调用并且返回值可以隐式转换为指定的类型。 #include iostream #include type_traitsint add(int a, int b) {return a b; }int main() {std::cout std::boolalpha;// 检查 add 是否可以用 int, int 调用并返回 intstd::cout Is add invocable with int, int and return int? std::is_invocable_r_vint, decltype(add), int, int std::endl;// 检查 add 是否可以用 int, int 调用并返回 doublestd::cout Is add invocable with int, int and return double? std::is_invocable_r_vdouble, decltype(add), int, int std::endl;return 0; }2. std::is_nothrow_invocable 和 std::is_nothrow_invocable_r std::is_nothrow_invocable 检查一个可调用对象是否可以使用给定的参数类型进行调用并且调用过程不会抛出异常。std::is_nothrow_invocable_r 则在此基础上还要求返回值可以隐式转换为指定的类型。 #include iostream #include type_traits// 不抛出异常的函数 void safe_foo(int x) noexcept {std::cout safe_foo called with x std::endl; }int main() {std::cout std::boolalpha;// 检查 safe_foo 是否可以用 int 调用且不抛出异常std::cout Is safe_foo nothrow invocable with int? std::is_nothrow_invocable_vdecltype(safe_foo), int std::endl;return 0; }五、使用场景 1. 模板元编程 在模板元编程中我们经常需要根据可调用对象的调用可行性来选择不同的实现路径。 #include iostream #include type_traitstemplate typename F, typename... Args, std::enable_if_tstd::is_invocable_vF, Args..., int 0 auto call_if_invocable(F f, Args... args) {return std::forwardF(f)(std::forwardArgs(args)...); }template typename F, typename... Args, std::enable_if_t!std::is_invocable_vF, Args..., int 0 void call_if_invocable(F, Args...) {std::cout Not invocable. std::endl; }void baz(int x) {std::cout baz called with x std::endl; }int main() {call_if_invocable(baz, 42);call_if_invocable([](double) {}, 10); // 这里不匹配调用输出 Not invocable.return 0; }2. 泛型算法 在编写泛型算法时我们可以使用 std::is_invocable 来确保传入的可调用对象符合算法的要求。 #include iostream #include vector #include type_traitstemplate typename Container, typename Func, std::enable_if_tstd::is_invocable_vFunc, typename Container::value_type, int 0 void apply(Container c, Func f) {for (auto elem : c) {f(elem);} }int main() {std::vectorint numbers {1, 2, 3, 4, 5};auto print [](int x) { std::cout x ; };apply(numbers, print);std::cout std::endl;return 0; }六、注意事项 隐式转换std::is_invocable 会考虑参数的隐式转换。例如如果一个函数接受 int 类型的参数那么传入 short 或 char 类型的参数也会被认为是可调用的因为存在隐式转换。成员函数指针在使用成员函数指针时需要注意传递合适的对象实例作为第一个参数。例如对于一个成员函数 void MyClass::func()调用时需要传递 MyClass 的实例或指针。 #include iostream #include type_traitsclass MyClass { public:void member_func() {std::cout Member function called. std::endl;} };int main() {std::cout std::boolalpha;// 检查成员函数指针是否可调用std::cout Is member_func invocable? std::is_invocable_vdecltype(MyClass::member_func), MyClass std::endl;return 0; }七、结论 std::is_invocable 系列类型特征为 C 程序员提供了强大的编译时检查能力使得我们可以在编写通用代码时更加安全和高效。通过合理使用 std::is_invocable 及其变体我们可以避免在运行时出现调用错误提高代码的健壮性和可维护性。同时在模板元编程和泛型算法中std::is_invocable 也发挥着重要的作用。
http://www.w-s-a.com/news/464027/

相关文章:

  • 装饰公司网站模板科技成果鉴定机构
  • 给公司做的东西放到私人网站上十堰为企业做网站的单位
  • 手机网站建设价钱手机自己做网站
  • 网站建设属于哪种公司电子商务查询网站
  • 工程建设标准强制性条文最新版本网站关键词排名优化应该怎么做
  • 网站网页设计内容品牌高端网站建设公司
  • 网站开发报价 福州中国建筑网官网手机版
  • 网站 图片 自动往右移专门做定制化的网站
  • 最好用的cms手机百度关键词排名 网站优化软件
  • 凉山州城乡规划建设局网站长沙网站建设哪家强
  • 广州网站开发创意设计公司企业自己怎么制作网站首页
  • 曲靖 曲靖网站建设软件(app)开发wordpress 没有远程发布
  • 官方网站开发与定制网站建设技术是干嘛的
  • 昆明网站建设工作室网站菜单导航怎么做的
  • 南京网站做的好的公司猪八戒网站做推广怎么样
  • 建站收费标准福州网站搭建
  • 做防护用品的网站欧美网站建设风格特点
  • 龙华做网站联系电话北京软件开发培训班
  • 做网站运营有前途网站的建设与管理的心得体会
  • 河南网站推广怎么做网页制作免费下载
  • 网站如何屏蔽中国ip商丘网站建设的公司哪家好
  • 东莞广告公司东莞网站建设价格鹤壁哪有做网站的
  • 门户网站界面设计logo设计商标设计
  • 建设银行网站驱动宁波网站建设相信荣胜网络
  • 八里河网站建设项目建设可行性企业品牌推广方式有哪些
  • jsp网站开发之html入门知识广州服装设计公司
  • 做电商看的网站有哪些个人网页制作成品免费
  • 沈阳建站多少钱境外网站 备案
  • 提交网站收录入口斗图在线制作
  • 建设化妆品网站服务医药网站前置审批