#include <array>
#include <cstdint>
#include <iostream> class MyClass
{
private:
uint32_t m_Member; public:
constexpr MyClass(uint32_t parameter)
: m_Member{parameter}
{
}
constexpr uint32_t GetValue() const
{
return m_Member;
}
}; int main()
{
constexpr uint32_t ARRAY_SIZE{ MyClass{ }.GetValue() };
std::array<uint32_t, ARRAY_SIZE> myArray{ , , , , };
for (auto&& number : myArray)
{
std::cout << number << std::endl;
}
return ;
}

C++有时候为了运行效率,提供了很多的feature,虽然比较难用

关键字constexpr 就是在编译时进行推导,提升运行效率。

Using constexpr to Define the Size of an array

 #include <array>
#include <cstdint>
#include <iostream> int main()
{
constexpr uint32_t ARRAY_SIZE{ };
std::array<uint32_t, ARRAY_SIZE> myArray{ , , , , };
for (auto&& number : myArray)
{
std::cout << number << std::endl;
}
return ;
}

Creating constexpr class Constructors

2-5. Working with Compile Time Constants的更多相关文章

  1. Modern C++ CHAPTER 2(读书笔记)

    CHAPTER 2 Recipe 2-1. Initializing Variables Recipe 2-2. Initializing Objects with Initializer Lists ...

  2. 最近编译POCO 库和 Boost库的笔记

    最近在编译POCO库和BOOST库 先讲一下编译POCO库,我编译的是1.9.0,过程相当曲折,要OPENSSL修改版本的,个OPENSSL在这里下载,如果你用一般未修改的OPENSSL 是编译不了, ...

  3. STL空间分配器源码分析(二)mt_allocator

    一.简介 mt allocator 是一种以2的幂次方字节大小为分配单位的空间配置器,支持多线程和单线程.该配置器灵活可调,性能高. 分配器有三个通用组件:一个描述内存池特性的数据,一个包含该池的策略 ...

  4. CLR via C# 3rd - 07 - Constants and Fields

    1. Constants        A constant is a symbol that has a never-changing value. When defining a constant ...

  5. 深入浅出OOP(五): C#访问修饰符(Public/Private/Protected/Internal/Sealed/Constants)

    访问修饰符(或者叫访问控制符)是面向对象语言的特性之一,用于对类.类成员函数.类成员变量进行访问控制.同时,访问控制符也是语法保留关键字,用于封装组件. Public, Private, Protec ...

  6. Effective Java 30 Use Enums instead of int constants

    Enumerated type is a type whose legal values consist of a fixed set of constants, such as the season ...

  7. 7.Constants and Fields

    1.Constants is a symbol that has a never-changing value.  its value must be determinable at compile ...

  8. compile time - run-time

    php.net Class member variables are called "properties". You may also see them referred to ...

  9. Vue源码详细解析:transclude,compile,link,依赖,批处理...一网打尽,全解析!

    用了Vue很久了,最近决定系统性的看看Vue的源码,相信看源码的同学不在少数,但是看的时候却发现挺有难度,Vue虽然足够精简,但是怎么说现在也有10k行的代码量了,深入进去逐行查看的时候感觉内容庞杂并 ...

随机推荐

  1. CSS继承

    不可继承的:display.margin.border.padding.background.height.min-height.max-height.width.min-width.max-widt ...

  2. CSS3知识点总结----属性选择器

    1.E[attr]只使用属性名,但没有确定任何属性值 2.E[attr="value"]指定属性名,并指定了该属性的属性值 3.E[attr~="value"] ...

  3. highCharts 饼图动态加载

    饼图的动态加载 (1):导入样式 <script type="text/javascript" src="<%=request.getContextPath( ...

  4. Tomcat部署web项目,虚拟目录,上下文(Context),WEB-INF,web.xml,servlet,404

    Web项目的uri模型大致如下: http://localhost:8080 (/context) (/resource) 站点/上下文/资源 一. Tomcat中指定上下文(Context) 方法一 ...

  5. DBTest/1.TestWrite fails: MDB_BAD_TXN: Transaction cannot recover - it must be aborted

    今天,终于把这个困扰我好久的问题解决了.心累之余,分享给大家. 主要问题是编译caffe的时候报错了: [----------] 5 tests from DBTest/1, where TypePa ...

  6. 《跑跑跑》(五)——添加障碍物,Tiled障碍层的使用

    [转]http://blog.csdn.net/u010778159/article/details/44036365 首先利用TiledMap在原来的地图上添加上障碍物,先新建两个图层,分别叫bar ...

  7. 一个简单的synchronized多线程问题、梳理与思考

    一个程序,多个线程同时操作一个变量,给这个变量+1().功能很简单,可是怎么样去实现呢?这其中涉及到了哪些问题? 最基础想法 见代码: public class Test extends Thread ...

  8. HDOJ(2438)几何里的三分

    Turn the corner http://acm.hdu.edu.cn/showproblem.php?pid=2438 题目:一辆车能否在一个路口拐弯,看图就很明白啦. 算法:见下图,只要求出图 ...

  9. 转:python dict按照value 排序

    我们知道Python的内置dictionary数据类型是无序的,通过key来获取对应的value.可是有时我们需要对dictionary中 的item进行排序输出,可能根据key,也可能根据value ...

  10. Diamond使用向导

    一.背景描述                     Diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是简单.可靠.易用,目前淘宝内部绝大多数系统的配置,由diamond来进行统一管理. ...