• C++11之constexpr


            注:大前提,本篇文章是在介绍C++11中的constexpr,自C++14以来constexpr有了非常大的改动,如在实验中遇见与本文不符的地方还先请查阅其他资料,确定为本文错误后可留言,我会虚心接受并改正。 

    constexpr定义编译时常量

            在C++11中添加了一个新的关键字constexpr,这个关键字是用来修饰常量表达式的。所谓常量表达式,指的就是由多个(≥1)常量(值不会改变)组成并且在编译过程中就得到计算结果的表达式。常量表达式和非常量表达式的计算时机不同,非常量表达式只能在程序运行阶段计算出结果,但是常量表达式的计算往往发生在程序的编译阶段,这可以极大提高程序的执行效率,因为表达式只需要在编译阶段计算一次,节省了每次程序运行时都需要计算一次的时间。在C++11中添加了constexpr关键字之后就可以在程序中使用它来修饰常量表达式,用来提高程序的执行效率。在使用中建议将 const 和 constexpr 的功能区分开,即凡是表达“只读”语义的场景都使用 const,表达“常量”语义的场景都使用 constexpr

            在定义常量时const和constexpr是等价的,都可以在程序的编译阶段计算出结果,并可以用来定义数组。

    1. const int size_c = 1024;
    2. constexpr int size_cpr = 2048;
    3. int main()
    4. {
    5. int array1[size_c] = { 1,2,3 };
    6. int array2[size_cpr] = { 3,4,5 };
    7. return 0;
    8. }
    '
    运行

            如果要定义一个结构体/类常量对象,可以这样写:

    1. #include
    2. using namespace std;
    3. template<class T>
    4. struct Test1
    5. {
    6. T t;
    7. int i;
    8. array<double, 3> arr; // array在栈上存数据
    9. };
    10. struct Test2
    11. {
    12. string str;
    13. vector<int> v;
    14. };
    15. int main()
    16. {
    17. constexpr Test1<char> t1{ 'a', 10, {1,2,3} }; // 必须要在定义时就赋值
    18. cout << t1.t << " " << t1.i << endl;
    19. int arr[t1.i] = {}; // 证实t1.i具有常量属性
    20. //t1.arr[0] = 10; //error constexpr修饰的对象的成员也是常量,不能进行修改
    21. //const/constexpr array a{ 5, 6, 7 };
    22. //t1.arr = a;
    23. //error 因为constexpr修饰的常量是在编译时确定的,而string、vector等容器的数据是存在堆上的,而程序对堆空间的管理是在运行时才开始的,所以会出现编译错误
    24. //constexpr Test2 t2{ "123", {1,2,3} };
    25. const Test2 t2{ "123", {1,2,3} }; // 这里的const不是声明t2为编译时常量(真常量),而是声明为运行时常量(只读属性)
    26. //t2.str = "000"; //error 只读对象的成员也是只读的
    27. return 0;
    28. }

    常量表达式函数

            为了提高C++程序的执行效率,我们可以将程序中值不需要发生变化的变量定义为常量,也可以使用constexpr修饰函数的返回值,这种函数被称作常量表达式函数,这些函数主要包括以下几种:普通函数/类成员函数、类的构造函数、模板函数。

            注:由于现在编译器版本都比较高,默认的使用的C++标准也比较高(大于C++11),相关源代码请基于 C++11 标准进行测试。

    普通函数/类的成员函数

            constexpr并不能修饰任意函数的返回值,使这些函数成为常量表达式函数,必须要满足以下几个条件:

            函数必须要有返回值,return返回的表达式必须是常量表达式,并且整个函数的函数体内有且只能有一条return语句,不能出现除此之外的语句。(using、typedef、static_assert语句除外)(C++14对该规则有较大修改)

    1. #include
    2. using namespace std;
    3. // error 没有返回值
    4. constexpr void test1()
    5. {}
    6. constexpr bool test2()
    7. {
    8. using my_type = int;
    9. typedef int new_type;
    10. static_assert(10 > 0, "10 not greater than 0");
    11. return true;
    12. }
    13. constexpr int test3(int i)
    14. {
    15. return i > 0 ? i : 0;
    16. }
    17. int main()
    18. {
    19. test1();
    20. test2();
    21. int i;
    22. cin >> i;
    23. cout << test3(i) << endl;
    24. return 0;
    25. }

            函数在使用之前,必须有对应的定义语句。 

    1. #include
    2. using namespace std;
    3. constexpr int test();
    4. int main()
    5. {
    6. // error 在定义前使用constexpr函数
    7. constexpr int res = test();
    8. cout << res << endl;
    9. return 0;
    10. }
    11. constexpr int test()
    12. {
    13. return 1;
    14. }

            注:以上规则不仅对应普通函数适用,对应类的成员函数也是适用的。


    构造函数 

             如果想用直接得到一个常量对象,也可以使用constexpr修饰一个构造函数,这样就可以得到一个常量构造函数了。常量构造函数有一个要求:构造函数的函数体必须为空,并且必须采用初始化列表的方式为各个成员赋值。

            当我们在类的构造函数前加上constexpr关键字时,‌我们是在向编译器表明,‌只要传递给构造函数的参数都是constexpr,‌那么该构造函数就可以在编译时期执行,‌产生的对象也将是constexpr对象。‌这意味着,‌这样的对象可以在编译时期被初始化,‌并且可以在只允许使用constexpr的场合中使用。‌例如,‌constexpr对象可以用作常量,‌它们可以在编译时被计算和赋值给常量变量或枚举值。‌

    1. #include
    2. using namespace std;
    3. struct Test
    4. {
    5. public:
    6. constexpr Test() : i(0), d(0.0), c('0')
    7. {}
    8. ~Test() = default;
    9. int i;
    10. double d;
    11. char c;
    12. };
    13. int main()
    14. {
    15. // 错误用法,这样使用构建出的对象不是常量
    16. Test t1;
    17. cout << t1.i << " " << t1.d << " " << t1.c << endl;
    18. t1.i = 100;
    19. cout << t1.i << " " << t1.d << " " << t1.c << endl;
    20. Test t2;
    21. t1 = t2;
    22. cout << t1.i << " " << t1.d << " " << t1.c << endl;
    23. // 正确用法
    24. constexpr Test t3;
    25. //t3.i = 100; // error
    26. return 0;
    27. }


    模板函数

            C++11 语法中,constexpr可以修饰函数模板,但由于模板中类型的不确定性,因此函数模板实例化后的模板函数是否符合常量表达式函数的要求也是不确定的。如果constexpr修饰的模板函数实例化结果不满足常量表达式函数的要求,则constexpr会被自动忽略,即该函数就等同于一个普通函数

    1. #include
    2. using namespace std;
    3. struct Person
    4. {
    5. const char* name_;
    6. uint16_t age_;
    7. };
    8. template<class T>
    9. constexpr T dispatch(const T& t)
    10. {
    11. return t;
    12. }
    13. int main()
    14. {
    15. // 因为12345是字面量即常量,所以此时dispatch是常量表达式函数
    16. constexpr int ret = dispatch(12345);
    17. Person p1{ "zhangsan", 15 };
    18. // 因为p1是变量,不是常量表达式,所以dispacth函数的constexpr失效
    19. Person p2 = dispatch(p1);
    20. cout << p2.name_ << " " << p2.age_ << endl;
    21. constexpr Person p3{ "zhangsan", 15 };
    22. // 因为p3为常量,所以此时dispacth函数的constexpr有效
    23. constexpr Person p4 = dispatch(p3);
    24. cout << p4.name_ << " " << p4.age_ << endl;
    25. return 0;
    26. }

  • 相关阅读:
    基于SVm和随机森林算法模型的中国黄金价格预测分析与研究
    在大厂工作是这样的
    Java——数组的使用
    2023-9-12 01背包问题
    React 封装 SVG 图标
    防火墙标准检查单
    做PPT绝对不能错过这5个网站
    Node 版本切换工具之 NVM 安装及使用教程
    ES7新特性深度解析:提升JavaScript开发效率的利器
    【C+】C++11 —— 线程库
  • 原文地址:https://blog.csdn.net/2301_80909286/article/details/140449188