IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> Understanding constexpr specifier in C++ -> 正文阅读

[C++知识库]Understanding constexpr specifier in C++

constexpr (C++) | Microsoft Docs

Understanding constexpr specifier in C++ - GeeksforGeeks

constexpr is a feature added in C++11. The main idea is performance improvement of programs by doing computations at compile time rather than run time. Note that once a program is compiled and finalized by developer, it is run multiple times by users. The idea is to spend time in compilation and save time at run time

constexpr specifies that the value of an object or a function can be evaluated at compile time and the expression can be used in other constant expressions. For example, in below code product() is evaluated at compile time.

constexpr int product(int x, int y)
{
    return (x*y);
}

int main()
{
    const int x = product(10, 20);
    cout << x;
    return 0;
}

output: 20

A function be declared as constexpr

1. In C++11, a constexpr function should contain only one return statement. C++14 allows more than one statements.

2. constexpr function should refer only constant global variables.

3. constexpr function can call only other constexpr function not simple function.

4. Function should not be of void type and some operator like prefix increment(++v) are not allowed in constexpr function.

constexpr vs inline functions:

Both are for performance improvments, inline functions request compiler to expand at compile time and save time of function call overheads. In inline functions, expression are always evaluated at run time. constexpr is different, here expressions are evaluated at compile time.

#include <iostream>
using namespace std;

constexpr long int fib(int n)
{
    return (n <= 1)? n : fib(n-1) + fib(n-2);
}

int main()
{
    const long int res = fib(30);
    cout << res;
    return 0;
}

constexpr with constructors:

#include <bits/stdc++.h>
using namespace std;

class Rectangle
{
    int _h, _w;
public:
    constexpr Rectangle(int h, int w): _h(h), _w(w) {}
    constexpr int getArea() { return _h * _w;}
};

int main()
{
    constexpr Rectange obj(10, 20);
    cout << obj.getArea();
    return 0;
}

constexpr vs const

They serve different purposes. constexpr is mainly for optimaization while const is for pratically const objects like value of Pi.

Both of them can be applied to member methods. Member methods are made const to make sure that there are no accident changes by the method. On the other hand, the idea of using consexpr is to compute expressions at compile time so that time can be saved when code is run.

const can only be used with non-static member function whereas constexpr can be used with member and non-member functions, even with constructors but with condition that argument and return type must be of literal types.

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-12-01 17:31:50  更:2021-12-01 17:32:12 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 8:43:51-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码