constexpr if --- C++ 20

constexpr if — C++ 20

constexpr if 可以让我们实现条件编译

template <typename T>
auto getResult(T t)
{if constexpr (std::is_integral_v<T>)return *t;elsereturn t;
}

如果Tintergral类型,执行第一个分支,否则执行第二个分支

还记得前文写过的模板元编程递归求N次方吗? 使用constexpr if可以使我们的代码更加的优雅

template <int N> 
constexpr int factorial()
{if constexpr (N >= 2)return N * factorial<N - 1>();elsereturn N;
}int main()
{cout << factorial<10>() << endl;
}

constexpr if 也会使得斐波那契数列更加的优雅


template <int N>
constexpr int fibonacci()
{if constexpr (N >= 2)return fibonacci<N - 1>() + fibonacci<N - 2>();elsereturn N;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407974.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

WPF中的动画

WPF中的动画 周银辉动画无疑是WPF中最吸引人的特色之一&#xff0c;其可以像Flash一样平滑地播放并与程序逻辑进行很好的交互。这里我们讨论一下故事板。在WPF中我们采用Storyboard&#xf…

c/c++面试试题(四)

華為1、局部变量能否和全局变量重名&#xff1f;答&#xff1a;能&#xff0c;局部会屏蔽全局。要用全局变量&#xff0c;需要使用"::"局部变量可以与全局变量同名&#xff0c;在函数内引用这个变量时&#xff0c;会用到同名的局部变量&#xff0c;而不会用到全局变量…

[访问系统] Api_Win32_Mac类工具包 (转载)

点击下载 Api_Win32_Mac.zip using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices;namespace BaseFunction {class API{[DllImport("kernel32")]//内存public static extern void GlobalM…

constexpr 函数

constexpr 函数 — C 20 constexpr double pi 3.14;constexpr允许你在编译时使用典型的C函数语法进行编程,但这并不意味之constexpr只和编译期有关 constexpr函数可以在编译期运行,也可以在运行时运行 但在以下情况constexpr函数必须在编译期运行: constexpr函数在编译的上…

react(82)--方法写在effects里面

effects: {*addOrUpdateAssistActivity({ payload, callback }, { call }) {try {const res yield call(addOrUpdateAssistActivity, payload);if (callback && typeof callback function) {yield callback(res);}} catch (error) {console.log(error);}},

让VS2005用起来更顺手

1. 在解决方案资源管理器中跟踪活动项VS2005 工具 选项 项目和解决方案 常规&#xff0c;把“在解决方案资源管理器中跟踪活动项”打勾。2. 显示行号VS2005 工具 选项 文本编辑器 C#&#xff0c;把“行号”打勾。或者 VS2005 工具 选项 文本编辑器 所有语言&…

C++面试题目(五)

思科1. 用宏定义写出swap&#xff08;x&#xff0c;y&#xff09;#define swap(x, y)/x x y;/y x - y;/x x - y;2.数组a[N]&#xff0c;存放了1至N-1个数&#xff0c;其中某个数重复一次。写一个函数&#xff0c;找出被重复的数字.时间复杂度必须为o&#xff08;N&#xff…

如何利用脚本方法清除文本框中汉字,从而保留英文字母和数字等

脚本方法如下&#xff1a; <script language"javascript" type"text/javascript"> function RemoveChineseCharacters(teleriktextboxID) {var textboxinput document.getElementById(teleriktextboxID);var textboxinputhidden document.g…

react(83)--filter

let newarr friendTableSource.filter(function(item, index, arr0) {return item.code ! val;});

constexpr和consteval --- C++ 20

constexpr和consteval — C 20 标准库容器和算法库对constexpr 的应用 C20 中大量的算法和容器可以使用constexpr,这意味着你甚至可以再编译期vector<int>进行排序 Algorithms library - cppreference.com 如下: #include <iostream> #include <ranges>…

西藏攻略集锦

西藏旅游局针对初此进藏旅游的驴友提供实用资讯本文为西藏旅游局针对初次进藏的驴友提供实用资讯&#xff0c;供各位参考。 1、什么是高原反应&#xff1f;高原反应有哪些症状&#xff1f; 高原反应是人到达一定海拔高度后&#xff0c;身体为适应因海拔高度…

将select中的项从一个移动到另一个select中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns"http://www.w3.org/1999/xhtml" ><head> <title>将select中的项互相移动…

函数模板(参考《C++ Templates 英文版第二版》)

函数编程(参考《C Templates 英文版第二版》) Chapter 1 函数模板 1.1 一窥函数模板 template<class T> T max(T a, T b) {return b < a ? a : b; }#include "max1.hpp" #include <iostream> #include <string> #include <format>int…

Android FTP Server 1

下面介绍几种Android 版本的FTP Server : virtualdataline Virtual Data Line is a software that you can manage files of the phone on you pc without data line . Phone files can be copied to a computer and computer files can copied to the phone. With this …

Switcher ---Vista Areo 工具

推荐一个Vista Areo主题工具Switcher.可以从这里 [url]http://insentient.net/[/url] 获取 自由、创新、研究、探索…… 转载于:https://blog.51cto.com/shanyou/74271

很好的测试智商看看你能回答出来多少?一共75道!!

【1】假设有一个池塘&#xff0c;里面有无穷多的水。现有2个空水壶&#xff0c;容积分别为5升和6升。问题是如何只用这2个水壶从池塘里取得3升的水。 【2】周雯的妈妈是豫林水泥厂的化验员。 一天&#xff0c;周雯来到化验室做作业。做完后想出去玩。 "等等&#xff0c;妈…

react(84)--多张图片

<Form.Item label"上传封面图片"><BaseUploadImageonRef{(ref) > {this.upload ref;}}value{coverPaths}multiple/></Form.Item>

【ES实战】Elasticsearch6开始的CCR

【ES实战】学习使用Elasticsearch6开始的CCR 本文涉及官网文章地址 OverviewRequirements for leader indicesAutomatically following indicesGetting started with cross-cluster replicationUpgrading clusters CCR > Cross-cluster replication 文章目录 【ES实战】学…

可变参数模板(参考《C++ Templates 英文版第二版》)

可变参数模板(参考《C Templates 英文版第二版》) Chapter 4 可变参数模板 自从C11,模板可以接受可变数量的参数 4.1 可变参数模板 可以定义模板,去接受无限数量的模板参数 这种行为的模板叫做可变参数模板 4.1.1 例子 #include <iostream>template<typename T…