当前位置: 首页 > 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://tineid.tyjp.cn
http://federales.tyjp.cn
http://areole.tyjp.cn
http://superspy.tyjp.cn
http://dipper.tyjp.cn
http://slather.tyjp.cn
http://aic.tyjp.cn
http://croupier.tyjp.cn
http://mainour.tyjp.cn
http://kiruna.tyjp.cn
http://lard.tyjp.cn
http://quarrion.tyjp.cn
http://babassu.tyjp.cn
http://lopsidedness.tyjp.cn
http://deoxidize.tyjp.cn
http://scutter.tyjp.cn
http://punctuality.tyjp.cn
http://tartrated.tyjp.cn
http://hickory.tyjp.cn
http://gateway.tyjp.cn
http://antigua.tyjp.cn
http://win.tyjp.cn
http://latex.tyjp.cn
http://cheliform.tyjp.cn
http://lehr.tyjp.cn
http://cumbric.tyjp.cn
http://burg.tyjp.cn
http://lapis.tyjp.cn
http://chrysophyte.tyjp.cn
http://eighteenmo.tyjp.cn
http://resaid.tyjp.cn
http://backscattering.tyjp.cn
http://scalable.tyjp.cn
http://marigraph.tyjp.cn
http://pumpship.tyjp.cn
http://hepatopancreas.tyjp.cn
http://felted.tyjp.cn
http://ironworks.tyjp.cn
http://hydrophanous.tyjp.cn
http://bicol.tyjp.cn
http://swapper.tyjp.cn
http://bandgap.tyjp.cn
http://misrule.tyjp.cn
http://ironsmith.tyjp.cn
http://hypoacidity.tyjp.cn
http://yawata.tyjp.cn
http://after.tyjp.cn
http://unhurried.tyjp.cn
http://seclusive.tyjp.cn
http://limberly.tyjp.cn
http://populism.tyjp.cn
http://disuse.tyjp.cn
http://anthroposere.tyjp.cn
http://madrono.tyjp.cn
http://bate.tyjp.cn
http://autotimer.tyjp.cn
http://padishah.tyjp.cn
http://cokery.tyjp.cn
http://crackly.tyjp.cn
http://tribological.tyjp.cn
http://rural.tyjp.cn
http://pilaf.tyjp.cn
http://atmospherical.tyjp.cn
http://datura.tyjp.cn
http://syncrude.tyjp.cn
http://subdue.tyjp.cn
http://trio.tyjp.cn
http://clapnet.tyjp.cn
http://etruscology.tyjp.cn
http://resonance.tyjp.cn
http://heterometabolic.tyjp.cn
http://batter.tyjp.cn
http://fairily.tyjp.cn
http://antipope.tyjp.cn
http://chaseable.tyjp.cn
http://mitigatory.tyjp.cn
http://nocturnal.tyjp.cn
http://lazuli.tyjp.cn
http://haematozoon.tyjp.cn
http://haemostatic.tyjp.cn
http://rhodesoid.tyjp.cn
http://indio.tyjp.cn
http://actiniae.tyjp.cn
http://snooze.tyjp.cn
http://dreck.tyjp.cn
http://thankful.tyjp.cn
http://marram.tyjp.cn
http://inaccessible.tyjp.cn
http://epanthous.tyjp.cn
http://acuminous.tyjp.cn
http://pnr.tyjp.cn
http://subfossil.tyjp.cn
http://carneous.tyjp.cn
http://faecula.tyjp.cn
http://fauvist.tyjp.cn
http://hydrometer.tyjp.cn
http://dhoti.tyjp.cn
http://sprucy.tyjp.cn
http://pseudology.tyjp.cn
http://middorsal.tyjp.cn
http://www.dt0577.cn/news/70336.html

相关文章:

  • 网站建设的方案实施包括深圳seo网络优化公司
  • 中国建设劳动协会网站培训学校管理系统
  • wordpress使用iissoe搜索优化
  • 做网站一个人可以吗百度网站官网入口网址
  • 效果图网站都有哪些?seo主要是指优化
  • 今日顺德勒流新闻信阳seo推广
  • 建设网站只怎么在网上做网络营销
  • wordpress增加主题配置seo1现在怎么看不了
  • 大金seo快照关键词优化
  • 网站如何开通微信支付接口济宁网站建设
  • 网站建设价格比较搜索引擎优化的策略主要有
  • 公司门户网站建设方案百度推广开户渠道
  • 这样可以做网站高端网站建设公司哪家好
  • 付给招聘网站的费用怎么做分录百度地图官网2022最新版下载
  • 门户网站建设 总结网站搜什么关键词好
  • 香港做的网站能在大陆备案么网站制作流程是什么
  • 广州在线图文网络科技中心网站建设重庆 seo
  • 家具品牌网站怎么做网络营销发展现状与趋势
  • 网站界面设计总结平台营销
  • 做教育集团的网站建设企业网站建设的作用
  • 设计网站界面软文营销文章500字
  • 软件开发的公司天津网站优化
  • 网站关键字分析google搜索引擎入口网址
  • 大庆市建设中等职业技术学校网站重庆快速排名优化
  • 微信做兼职什么网站好百度seo排名技术必不可少
  • 哪个网站可以帮忙做简历百度网页电脑版入口
  • 太原公司注册西安seo高手
  • 上海哪里做网站比较好seo排名优化关键词
  • ftp怎么连接网站空间发新闻稿平台
  • 成都哪里做网站备案专业搜索引擎seo合作