1.作用

  constexpr 声明一个函数或变量,它的值可以在编译时出现在常量表达式之中。

2.constexpr 变量要求

  • 其类型必须是 字面类型 (LiteralType) 。
  • 它必须被立即初始化
  • 其初始化的全表达式,包括所有隐式转换、构造函数调用等,都必须是常量表达式.

3.constexpr 普通函数要求

  • 必须非虚.(C++20 前)
  • 返回类型必须是字面类型 (LiteralType)
  • 其每个参数都必须是字面类型 (LiteralType)
  • 至少存在一组实参值。
  • 函数体必须不含 (C++14 后)
    1. asm 声明
    2. goto 语句
    3. 拥有除 case 和 default 之外的标号的语句
    4. try 块 (C++20 前)
    5. 非字面类型的变量定义
    6. 静态或线程存储期变量的定义
    7. 不进行初始化的变量定义。
(=default; 或 =delete; 的函数体不含任何上述内容。)
  • 函数体必须被弃置或预置,或只含有下列内容:(C++14 前)
    1. 空语句(仅分号)
    2. static_assert 声明
    3. 不定义类或枚举的 typedef 声明及别名声明
    4. using 声明
    5. using 指令
    6. 恰好一条 return 语句。

4.constexpr构造函数的要求

  • 其每个参数都必须是字面类型 (LiteralType)
  • 不能有虚继承
  • 该构造函数必须无函数 try 块
  • 构造函数体必须满足 constexpr 函数体的制约
  • 对于 class 或 struct 的构造函数,每个子对象和每个非变体非 static 数据成员必须被初始化。若类是联合体式的类,对于其每个非空匿名联合体成员,必须恰好有一个变体成员被初始化
  • 对于非空 union 的构造函数,恰好有一个非静态数据成员被初始化
  • 每个被选用于初始化非静态成员和基类的构造函数必须是 constexpr 构造函数。
  • 构造函数体必须被弃置或预置,或只含有下列内容:
    1. 空语句
    2. static_assert 声明
    3. 不定义类或枚举的 typedef 声明及别名声明
    4. using 声明
    5. using 指令

5.constexpr if

  • 以 if constexpr 开始的语句被称为constexpr if 语句
  • 在 constexpr if (条件)语句中,条件的值必须是可转换到 bool 类型的常量表达式。
  • 若其值为 true,则抛弃 false分支语句(若存在),否则抛弃true分支语句。

6.示例

 #include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <vector> static void
fun(){
//printf("%s called \n",__func__);
} constexpr int
getCount(){
int a = ;
int b = ;
a *= b;
//printf("%s called \n",__func__); //error,C++11 constexpr 函数必须把一切放在单条 return 语句中,C++14 无此要求
//fun(); //error,fun 不是constexpr
return * ;
} class person{
protected:
virtual void getName();
}; class student : /*virtual*/ public person{ // 不能有虚继承
private:
constexpr static int LEN = ;
char16_t name[getCount()]; //getCount() 是constexpr函数。
uint8_t age;
public:
constexpr student():name(u"hello"),age(getCount()){
typedef int INT;
using namespace std;
enum Color{
Red,Green,Blue,
};
Color color = (Color);
}
student(int ){
}
/*
virtual constexpr int getAge(){ //error,constexpr成员函数不能是virtual的。
return 100;
}
*/
virtual void getName(){ }
friend constexpr char16_t* getName(student &,int max = );
}; constexpr char16_t*
getName(student &s,int max){
using namespace std;
typedef class student Student;
//static int value = 100; //error,不能有静态变量。
//vector<int> vec; //error,不是常量表达式。
if(s.age < ){
//goto END; //error,不能有goto
} //__asm__("movl %eax, %ebx\n\t"); //error,不能有汇编语句。 /*
try{ //error,不能有try...catch语句。
++s.age;
}catch(const exception &e){
printf("exception \n");
}
*/ switch(s.age){
case : return u"ek"; /* U"0" */; // U是char32_t,u是char16_t
case : return u"fel";
default : return u"Default";
}
END:
return u"error";
} int
main(int argc,char *argv[]){ if constexpr(getCount() > ){
#line 1024 "test.cpp"
}
printf("file = %s,line = %d\n",__FILE__,__LINE__);
return ;
}

c++新特性实验(3)声明与定义:constexpr的更多相关文章

  1. c++新特性实验(5)声明与定义:属性列表(C++11 起)

    1.初识属性 1.1 实验A: noreturn 属性 [[ noreturn ]] static void thread1(void *data){ cout << "nore ...

  2. c++新特性实验(4)声明与定义:右值引用(C++11)

    1.作用 c++11以前,临时对象.字面常量一般情况下不可以再次访问,也不可以修改.右值引用可以解决这个问题. 1.1 实验A #include <iostream> using name ...

  3. MySQL8.0新特性实验1

    Server层,选项持久化 mysql> show variables like '%max_connections%';+------------------------+-------+| ...

  4. c++新特性实验(1)预处理

    1.参考资料 1.1 C++ C++17 标准文档(正式)  :    https://www.iso.org/standard/68564.html C++ 标准文档(草案)      :   ht ...

  5. c++新特性实验(2)类型特性

    1. 基本类型 1.1 增加 long long long long int signed long long signed long long int unsigned long long unsi ...

  6. C++ 11 新特性:函数声明auto

    1.概览 1.1 函数名中的箭头,用来表明函数的return type,其使用在函数的返回类型需要通过模板参数进行推导,使用在decltype()和declval()不方便的场景 2.正文 c++ 中 ...

  7. C++11新特性实验

    #include <iostream> #include <vector> #include <map> #include <string> #incl ...

  8. php5.3到php7.0.x新特性介绍

    <?php /*php5.3*/ echo '<hr>'; const MYTT = 'aaa'; #print_r(get_defined_constants()); /* 5.4 ...

  9. PHP 7 新特性

    PHP 7 新特性 标量类型声明 PHP 7 中的函数的形参类型声明可以是标量了.在 PHP 5 中只能是类名.接口.array 或者 callable (PHP 5.4,即可以是函数,包括匿名函数) ...

随机推荐

  1. shell脚本,循环的记录

    ######################################################################### # File Name: showlogged.sh ...

  2. 最全Linux常用命令大全

    查看系统系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMB ...

  3. 锋利的Jquery(点击显示隐藏div)

    点击显示隐藏div <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  4. 查看python安装位置和已安装库的相关操作

    打开cmd.exe, *查看python安装位置 where python *查看已安装库 pip list 或者pip freeze *查看可以更新的第三方库 pip list --outdated ...

  5. 【9.14NOIP模拟pj】wtaxi 题解——搜索

    [9.14NOIP模拟pj]wtaxi 题目简化 有K辆车,N个人,上车给D元,只有S分钟.上车后无论多少人都要给D元,原地等多少分钟就没了多少元.求最小花费的钱. 我的思路 毫无疑问,此题可以用搜索 ...

  6. Ubuntu+Ruby+MySQL+Nginx+Redmine部署记录

    (2019年2月19日注:这篇文章原先发在自己github那边的博客,时间是2016年7月26日) 周五的时候老大布置了一个任务下来,要部署一个Redmine用于研发部,同时升级工作室的Redmine ...

  7. java基础之Date类

    Date类: Date类概述 类 Date 表示特定的瞬间,精确到毫秒. 构造方法 public Date() public Date(long date) 成员方法 public long getT ...

  8. 关于Server.MapPath和HostingEnvironment.MapPath

    最近做的项目需要在Controller里重写一个static的方法,在方法内用常用的Server.MapPath会报一个错误:An object reference is required for t ...

  9. Markdown 格式标记符号说明

    Markdown 格式标记符号说明 1. 标题 在行首插入 1 到 6个#,分别表示标题 1 到标题 6 # 这是标题1 ## 这是标题1 ###### 这是标题6 点击保存后的效果: 标题1 标题2 ...

  10. Android 开发 屏幕常亮的3个方法

    第一种 xml文件中的顶层布局添加属性: android:keepScreenOn="true" 第二种 在Window设置flag: getWindow().addFlags(W ...