一、C++ const 和 constexpr 的区别?

constexpr 表示这玩意儿在编译期就可以算出来(前提是为了算出它所依赖的东西也是在编译期可以算出来的)。

const 只保证了运行时不直接被修改(但这个东西仍然可能是个动态变量)。

constexpr 是 C++11 引入的,一方面是为了引入更多的编译时计算能力,另一方面也是解决 C++98 的 const 的双重语义问题。

const修饰的是类型,constexpr修饰的是用来算出值的那段代码。

在 C 里面,const 很明确只有「只读」一个语义,不会混淆。C++ 在此基础上增加了「常量」语义,也由 const 关键字来承担,引出来一些奇怪的问题。C++11 把「常量」语义拆出来,交给新引入的 constexpr 关键字。

二、为什么用 constexpr

预判错误

int i; // not constant
const int size = i; // fine! 可以,但为什么不在这里就先判断出问题的隐患呢? int arr[size]; // Error!

然而对于constexpr,则表明这个值不仅是constant的,而且也是编译期确定的

int i; // not constant
constexpr int size = i; // Error!

于是,constexpr修饰的变量是可以表示数组大小的。

数组初始化

constexpr可以用来修饰变量、函数、构造函数。一旦以上任何元素被constexpr修饰,那么等于说是告诉编译器 “请大胆地将我看成编译时就能得出常量值的表达式去优化我”。编译器优化过程中便会发现错误。

const int func() {
return ;
}
main(){
int arr[func()];
}
//error : 函数调用在常量表达式中必须具有常量值

告诉编译器返回的是个“常数”,所以,不会报错了。

constexpr func() {
return ;
}
main(){
int arr[func()];
}
//编译通过

为了性能

放在 stack上的数组也是可以的,比如main中的如下:
int main()
{
int i;
cin >> i;
int arr[i];
}

跟const无关,而是使用了C99的一个特性,名叫variable length array(简称VLA)。

而为什么我们需要constexpr呢?那就是为了性能。

其他链接

When should you use constexpr capability in C++11?

When should literal classes be used in C++?

Want speed? Use constexpr meta-programming! [static的方式是最快的]

三、字面类型 (LiteralType)

字面量 类型

Ref: C++ literal type

要区分 literal 和 literal-type 这两个不同的概念。

literal:文字量,10,3.14, true ,u8"123",  L"好"这些东西。

literal-type: 参考http://en.cppreference.com/w/cpp/concept/LiteralType  简单的说,就可以在用于编译期运算的对象。

标量 类型

对于标量,例如int,显然可以参与 编译期运算,例如:constexpr int fac( int N);  //计算阶乘。所以标量都是属于literal-type。

从这里可以看出,literal-type仅仅是类型系统中,一个catalog。所有的类型,要么归类到literal-type,要么归类到none-literal-type。

现在class,也能归类于literal-type,只要满足一些条件:

是否是 literal type?

// is_literal_type example
#include <iostream>
#include <type_traits> struct A { };
struct B { ~B(){} }; int main() {
std::cout << std::boolalpha;
std::cout << "is_literal_type:" << std::endl;
std::cout << "int: " << std::is_literal_type<int>::value << std::endl;
std::cout << "int&: " << std::is_literal_type<int&>::value << std::endl;
std::cout << "int*: " << std::is_literal_type<int*>::value << std::endl;
std::cout << "A: " << std::is_literal_type<A>::value << std::endl;
std::cout << "B: " << std::is_literal_type<B>::value << std::endl;
return ;
}

Output:

is_literal_type:
int: true
int&: true
int*: true
A: true
B: false

End.

[c++] constexpr and literal class的更多相关文章

  1. [Code::Blocks] Install wxWidgets & openCV

    The open source, cross platform, free C++ IDE. Code::Blocks is a free C++ IDE built to meet the most ...

  2. 本人SW知识体系导航 - Programming menu

    将感悟心得记于此,重启程序员模式. js, py, c++, java, php 融汇之全栈系列 [Full-stack] 快速上手开发 - React [Full-stack] 状态管理技巧 - R ...

  3. Item 15: 只要有可能,就使用constexpr

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 如果说C++11中有什么新东西能拿"最佳困惑奖" ...

  4. c++ 11 游记 之 decltype constexpr

    title: c++ 11 游记 1 keyword :c++ 11 decltype constexpr 作者:titer1 zhangyu 出处:www.drysaltery.com 联系:130 ...

  5. C++11特性——变量部分(using类型别名、constexpr常量表达式、auto类型推断、nullptr空指针等)

    #include <iostream> using namespace std; int main() { using cullptr = const unsigned long long ...

  6. format not a string literal and no format arguments

    今天cocos2d-x打包 android的时候报错:format not a string literal and no format arguments 报错点是:__String::create ...

  7. Android studio2.2 ndk 错误 :format not a string literal and no format arguments!

    在Android Studio2.2 进行NDK编程,在对*char 字符串 进行日志输出时,报错: error: format not a string literal and no format  ...

  8. asp.net Literal

    常用于动态向页面添加内容 Panel panel = new Panel(); Literal literal = new Literal(); literal.Text = "<br ...

  9. C++11:新式的字符串字面常量(String Literal)

    自C++11起,我们可以定义 raw string 字符串字面常量. Raw string 允许我们定义所见即所得的字符串字面常量,从而可以省下很多用来修饰特殊 字符的符号. Raw string 以 ...

随机推荐

  1. DIV+CSS自适应布局

    自适应布局分两类:高度和宽度,方法有很多,我用三列布局举例,我就列几个通俗易懂的例子呗,懂了三列的,两列的原理一样,呵呵哒. 效果图如下:高度自适应——宽度自适应            1,高度自适应 ...

  2. [IOS]使用了cocoapods 抱错Pods was rejected as an implicit dependency for ‘libPods.a’ because its architectures ......

    Pods was rejected as an implicit dependency for ‘libPods.a’ because its architectures ‘i386’ didn’t ...

  3. iOS在线更新framework,使用NSBundle动态读取

    官方文档:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingCode/Tasks/Loadin ...

  4. 学习笔记:因为java匿名类学习到接口的一些小用法

    在看CometD的示例代码时发现了许多有意思的代码,但说实话看别人的代码确实是件很累的事情,所以就看到这个知识点做一下记录吧.   先看一段代码: 代码1   这段代码中有一个new的操作,而且是在方 ...

  5. 简化 Web 应用程序与 Windows Azure Active Directory、ASP.NET 和 Visual Studio 的集成

    大家好! 今天的博文深入讨论我们今天推出的开发人员工具和框架中的一些新功能.我们通过与 ASP.NET 和 Visual Studio 团队合作开发了一些重大的增强功能,让开发人员能够轻松使用 Win ...

  6. 一个App完成入门篇(六)- 完成通讯录页面

    第五章和第六章间隔时间有点长,对不起大家了.下面继续. 本节教程将要教会大家如何加载本地通讯录. 导入项目 导入通讯录 自定义js模块 发送和订阅page消息 将要学习的demo效果图如下所示 1. ...

  7. 多线程中的锁系统(三)-WaitHandle、AutoResetEvent、ManualResetEvent

    本章主要介绍下基于内核模式构造的线程同步方式,事件,信号量. 阅读目录: 理论 WaitHandle AutoResetEvent ManualResetEvent 总结 理论 Windows的线程同 ...

  8. Mac 软件篇

    对于美好事务的追求无论何时都不算晚. ** 文章内容来着我整理的fetool,以下内容可能更新不及时 ** Mac 下的软件那么多,又是免费又是付费,应该怎么选呢?我来分享下我的推荐列表,推荐的优先级 ...

  9. 【Prince2是什么】PRINCE2认证之项目四大管理步骤

    昨天谈到PRINCE2要求项目经理在做项目的时候要考虑四大核心指标(成本.时间.质量.范围)加风险与收益这两个重要要素. 然后PRINCE2基于这几大要素进行了四大管理步骤,分别是: 1.计划 2.授 ...

  10. Atiti 数据库系统原理 与数据库方面的书籍 attilax总结 v3 .docx

    Atiti 数据库系统原理 与数据库方面的书籍 attilax总结 v3 .docx 1.1. 数据库的类型,网状,层次,树形数据库,kv数据库.oodb2 1.2. Er模型2 1.3. Sql2 ...