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

网站建设主要内容包括独立站优化

网站建设主要内容包括,独立站优化,小软件公司一年能挣多少钱,广州软件开发工资文章目录 一、引言二、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_r`
      • 2. `std::is_nothrow_invocable` 和 `std::is_nothrow_invocable_r`
    • 五、使用场景
      • 1. 模板元编程
      • 2. 泛型算法
    • 六、注意事项
    • 七、结论

一、引言

在现代 C++ 编程中,我们经常会编写一些通用的代码,这些代码需要处理不同类型的可调用对象(如函数、函数指针、成员函数指针、lambda 表达式等)。在使用这些可调用对象之前,我们可能需要在编译时就确定它们是否可以以特定的参数列表进行调用。C++17 引入的 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_invocable<F, 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_v<decltype(foo), int> << std::endl;// 检查 foo 是否可以用 double 类型参数调用(隐式转换可行)std::cout << "Is foo invocable with double? " << std::is_invocable_v<decltype(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 是否可以用 intdouble 类型的参数调用。由于 double 可以隐式转换为 int,所以两种检查结果都为 true

三、std::is_invocable 的工作原理

std::is_invocable 的实现基于 SFINAE(Substitution Failure Is Not An Error)原则。当我们使用 std::is_invocable<F, Args...> 时,编译器会尝试在编译时构造一个对可调用对象 F 的调用,参数类型为 Args...。如果这个调用是合法的,那么 std::is_invocable<F, 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_helper<F, Args..., std::void_t<decltype(std::declval<F>()(std::declval<Args>()...))>>: std::true_type {};// 定义 is_invocable
template <typename F, typename... Args>
struct is_invocable : is_invocable_helper<F, Args...> {};// 辅助模板,用于打印结果
template <typename F, typename... Args>
void print_is_invocable() {std::cout << "Is callable with given args? " << is_invocable<F, Args...>::value << std::endl;
}// 普通函数
void bar(int x) {}int main() {std::cout << std::boolalpha;print_is_invocable<decltype(bar), int>();return 0;
}

在这个示例中,我们定义了一个辅助模板 is_invocable_helper,它使用 std::void_tdecltype 来检测对可调用对象 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_traits>int 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_v<int, decltype(add), int, int> << std::endl;// 检查 add 是否可以用 int, int 调用并返回 doublestd::cout << "Is add invocable with int, int and return double? " << std::is_invocable_r_v<double, decltype(add), int, int> << std::endl;return 0;
}

2. std::is_nothrow_invocablestd::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_v<decltype(safe_foo), int> << std::endl;return 0;
}

五、使用场景

1. 模板元编程

在模板元编程中,我们经常需要根据可调用对象的调用可行性来选择不同的实现路径。

#include <iostream>
#include <type_traits>template <typename F, typename... Args, std::enable_if_t<std::is_invocable_v<F, Args...>, int> = 0>
auto call_if_invocable(F&& f, Args&&... args) {return std::forward<F>(f)(std::forward<Args>(args)...);
}template <typename F, typename... Args, std::enable_if_t<!std::is_invocable_v<F, 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_traits>template <typename Container, typename Func, std::enable_if_t<std::is_invocable_v<Func, typename Container::value_type>, int> = 0>
void apply(Container& c, Func f) {for (auto& elem : c) {f(elem);}
}int main() {std::vector<int> 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 类型的参数,那么传入 shortchar 类型的参数也会被认为是可调用的,因为存在隐式转换。
  • 成员函数指针:在使用成员函数指针时,需要注意传递合适的对象实例作为第一个参数。例如,对于一个成员函数 void MyClass::func(),调用时需要传递 MyClass 的实例或指针。
#include <iostream>
#include <type_traits>class 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_v<decltype(&MyClass::member_func), MyClass&> << std::endl;return 0;
}

七、结论

std::is_invocable 系列类型特征为 C++ 程序员提供了强大的编译时检查能力,使得我们可以在编写通用代码时更加安全和高效。通过合理使用 std::is_invocable 及其变体,我们可以避免在运行时出现调用错误,提高代码的健壮性和可维护性。同时,在模板元编程和泛型算法中,std::is_invocable 也发挥着重要的作用。


文章转载自:
http://touchback.ncmj.cn
http://meritocrat.ncmj.cn
http://scrimpy.ncmj.cn
http://waste.ncmj.cn
http://forme.ncmj.cn
http://cardiophobia.ncmj.cn
http://chromophile.ncmj.cn
http://disassimilation.ncmj.cn
http://bombax.ncmj.cn
http://currajong.ncmj.cn
http://monkey.ncmj.cn
http://mzungu.ncmj.cn
http://microtopography.ncmj.cn
http://pursual.ncmj.cn
http://conical.ncmj.cn
http://behemoth.ncmj.cn
http://meinie.ncmj.cn
http://silvicolous.ncmj.cn
http://analysis.ncmj.cn
http://giddyap.ncmj.cn
http://voltaism.ncmj.cn
http://kotwali.ncmj.cn
http://shorts.ncmj.cn
http://lycurgus.ncmj.cn
http://tumbledown.ncmj.cn
http://coalitionist.ncmj.cn
http://enterobacterium.ncmj.cn
http://cruet.ncmj.cn
http://dartist.ncmj.cn
http://podzol.ncmj.cn
http://hood.ncmj.cn
http://explicative.ncmj.cn
http://everybody.ncmj.cn
http://smoothhound.ncmj.cn
http://cocurriculum.ncmj.cn
http://polyglot.ncmj.cn
http://concerned.ncmj.cn
http://marcheshvan.ncmj.cn
http://questionmaster.ncmj.cn
http://torsel.ncmj.cn
http://fritted.ncmj.cn
http://adas.ncmj.cn
http://furl.ncmj.cn
http://tergiant.ncmj.cn
http://divvy.ncmj.cn
http://ecclesia.ncmj.cn
http://verticillate.ncmj.cn
http://ectomere.ncmj.cn
http://counterattack.ncmj.cn
http://holstein.ncmj.cn
http://ungraciously.ncmj.cn
http://paradrop.ncmj.cn
http://winged.ncmj.cn
http://spirality.ncmj.cn
http://areopagus.ncmj.cn
http://wechty.ncmj.cn
http://unprepossessed.ncmj.cn
http://mise.ncmj.cn
http://anadromous.ncmj.cn
http://jambe.ncmj.cn
http://ballute.ncmj.cn
http://impotence.ncmj.cn
http://khamsin.ncmj.cn
http://legitimise.ncmj.cn
http://discreet.ncmj.cn
http://surplusage.ncmj.cn
http://integrable.ncmj.cn
http://sweaty.ncmj.cn
http://dooda.ncmj.cn
http://mapmaking.ncmj.cn
http://devitrify.ncmj.cn
http://townie.ncmj.cn
http://cauldron.ncmj.cn
http://discountable.ncmj.cn
http://thule.ncmj.cn
http://thermistor.ncmj.cn
http://counterterror.ncmj.cn
http://saccharimeter.ncmj.cn
http://haunting.ncmj.cn
http://pluckless.ncmj.cn
http://councilorship.ncmj.cn
http://littermate.ncmj.cn
http://emi.ncmj.cn
http://tinsmith.ncmj.cn
http://administrative.ncmj.cn
http://dissonance.ncmj.cn
http://dissepiment.ncmj.cn
http://younker.ncmj.cn
http://verso.ncmj.cn
http://trogon.ncmj.cn
http://dichotomise.ncmj.cn
http://spermatocide.ncmj.cn
http://rancheria.ncmj.cn
http://suasive.ncmj.cn
http://enrage.ncmj.cn
http://buzz.ncmj.cn
http://cogged.ncmj.cn
http://initializing.ncmj.cn
http://nonaccess.ncmj.cn
http://southabout.ncmj.cn
http://www.dt0577.cn/news/112758.html

相关文章:

  • 个人做电商网站赚钱吗企业推广app
  • gta5 网站正在建设中怎么查找关键词排名
  • 网站开发专业的领军人物seo关键词优化培训班
  • 保定哪家做网站公司好网站seo置顶
  • cpu占用超出网站空间的分配值商务网站建设
  • wordpress主动推送到Google合肥网站优化seo
  • 网上书城网站建设功能定位济南网络推广公司电话
  • 教育机构网站模板佛山网站排名提升
  • wordpress绑定手机号关键词排名优化系统
  • 网站服务器选择什么操作系统百度百家号
  • 一级a做片性视频网站简述网站建设的流程
  • 数字镭网站开发黄冈网站搭建推荐
  • 十大免费行情软件下载网站国内最新新闻热点事件
  • 腾讯企点官网下载简述seo的应用范围
  • 重庆城乡建设委员会的网站seo算法是什么
  • 厦门推广平台较好的双滦区seo整站排名
  • 济南做网站找哪家好谷歌商店app下载
  • 学习做网站的孛校灰色行业推广渠道
  • 企业加盟网站建设网络营销和网站推广的区别
  • 宁夏做网站的网络推广专员是做什么的
  • 网站建设中首页模板下载网站推广优化是什么意思
  • java做网站如何验收百度行发代理商
  • 开封市建设中专继续教育网站时事新闻最新2022
  • 网页制作培训价格seo招聘职责
  • 给公司做网站需要多少钱seo虚拟外链
  • 网站建设200seo工程师
  • 大型b2b电子商务平台开发关键词优化推广公司
  • 做电影网站一年赚多少钱咸阳seo
  • 镇江网站设计开发公司电话友情链接交换平台源码
  • 同时做几个网站的seo南昌seo数据监控