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. LeetCode 28.实现strStr()(Python3)

    题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...

  2. Crontab- Linux必学的60个命令

    1.作用 使用crontab命令可以修改crontab配置文件,然后该配置由cron公用程序在适当的时间执行,该命令使用权限是所有用户. 2.格式 crontab [ -u user ] 文件 cro ...

  3. 基于VGGnet的人脸识别系统-ubuntu 系统下的Caffe环境搭建(CPU)

    对于caffe的系统一般使用linux系统,当然也有windows版本的caffe,不过如果你一开始使用了windows下面的caffe,后面学习的过程中,会经常遇到各种错误,网上下载的一些源码.模型 ...

  4. 全面理解python中self的用法

    self代表类的实例,而非类. class Test: def prt(self): print(self) print(self.__class__) t = Test() t.prt() 执行结果 ...

  5. Powerdesigner 生成数据字典

    https://www.jianshu.com/p/f491d0d3c503http://blog.csdn.net/adparking/article/details/50402980http:// ...

  6. [复习]平衡树splay

    明天要考试了…… 出来写一个splay的复习总结. 怕忘…… ^废话^ 以下内容学习自yyb大神的博客, 由于yyb大神内容不全, 部分是博主本人自行脑补... 这个模板还是比较全的^-^ ^又是一堆 ...

  7. 高斯消元+期望dp——light1151

    高斯消元弄了半天没弄对.. #include<bits/stdc++.h> using namespace std; #define maxn 205 #define eps 1e-8 d ...

  8. ros Python找不到msg包的问题解决办法

    https://answers.ros.org/question/113671/catkin-package-cannot-find-own-message-type-python/ 原因是因为.py ...

  9. natapp出现Invalid Host header

    前端是vue 3.x 项目,需要更改vue的配置文件 vue.config.js,在module.exports中添加devServer:{disableHostCheck:true}

  10. git difff

    Generate patch through git diff http://stackoverflow.com/questions/1191282/how-to-see-the-changes-be ...