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. Parted:2T以上磁盘分区工具(LINUX挂载2T以上磁盘)

    支持大于2T的磁盘,2T以下的最好还是用Fdisk来分区. [root@centos57 aixi]# parted /dev/hda print Model: VMware Virtual IDE ...

  2. 纵览轻量化卷积神经网络:SqueezeNet、MobileNet、ShuffleNet、Xception

    近年提出的四个轻量化模型进行学习和对比,四个模型分别是:SqueezeNet.MobileNet.ShuffleNet.Xception. SqueezeNet https://arxiv.org/p ...

  3. 动软DbHelperSQL

    using System; using System.Collections; using System.Data; using System.Data.SqlClient; using System ...

  4. Jmeter教程 简单的压力测试【转】

    Jmeter教程 简单的压力测试[转] Jmeter是一个非常好用的压力测试工具.  Jmeter用来做轻量级的压力测试,非常合适,只需要十几分钟,就能把压力测试需要的脚本写好. 阅读目录 什么是压力 ...

  5. LUOGU P2831 愤怒的小鸟 (NOIP 2016)

    题面 题解 好像昨天wxl大爷讲的是O(Tn*2^n)的做法,后来没想通,就自己写了个O(Tn^2*2^n)的暴力状压, 莫名其妙过了??数量级二十亿??懵逼,可能到了CCF老爷机上就T了.dp[S] ...

  6. Java—重写与重载的区别

    1.重写(Override) 子类继承了父类原有的方法,但有时子类并不想原封不动的继承父类中的某个方法,所以在方法名,参数列表,返回类型(除了子类中方法的返回值是父类中方法返回值的子类时)都相同的情况 ...

  7. idea 开始java之旅

    1.安装idea 2018.3.5 https://www.jetbrains.com/idea/ 2.破解安装教程 https://blog.csdn.net/qq_34668897/article ...

  8. https证书加密

    对称加密 浏览器向服务端发送请求时,服务端首先给浏览器发送一个秘钥,浏览器用秘钥对传输的数据进行加密后发送给浏览器,浏览器拿到加密后的数据使用秘钥进行解密 非对称加密 服务端通过rsa算法生成一个公钥 ...

  9. PKU OJ A Bug's life

    http://bailian.openjudge.cn/tm2018/G/ #include <iostream> #include <vector> #include < ...

  10. mysql设置密码登录

    参考: https://blog.csdn.net/Light_Breeze/article/details/82070222 https://www.jianshu.com/p/d979df2791 ...