#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. What's going on in background?

    Did you know that mobile phone manufacturer collect your info without notifying you? Did you know yo ...

  2. java 获取当月第一天和最后一天 获取前一个月第一天和最后一天

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");                    //获取前月的第一 ...

  3. ue4 c++ 接口

    使用UE4接口比起普通的高级语言,要多做很多工作,是因为要兼容蓝图的使用,有一些小坑需要注意,开始吧. 1.新建接口类 打开UE4编辑器,与往常一样,新建C++类,然后选择Object继承,然后取名字 ...

  4. 常用到的git,mvn,postgres,vim命令总结

    mvn: 打包: mvn package 如果想在打包的时候跳过测试: mvn package -Dmaven.test.skip=true 使用的junit测试框架, 测试: mvn test 如果 ...

  5. JCL笔记

    本文转自<http://leowzy.iteye.com/blog/888931> ---------------------------------------------------- ...

  6. Rsa加解密Java、C#、php通用代码 密钥转换工具

    之前发了一篇"TripleDes的加解密Java.C#.php通用代码",后面又有项目用到了Rsa加解密,还是在不同系统之间进行交互,Rsa在不同语言的密钥格式不一样,所以过程中主 ...

  7. cxf client端借口类型找不到问题

    问题: log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Exception in ...

  8. python3.5------day3-数据结构(dict,file)

    字典(dict) 字典的定义: 字典的形式是以key:values.{key1,values,key2,values} 特性: 1.可以存放多个值 2.字典是无需的 3.字典的key是唯一,有去重功能 ...

  9. Error: Cannot open main configuration file '//start' for reading! 解决办法

    当执行service nagios start启动nagios时,报错:Error: Cannot open main configuration file '//start' for reading ...

  10. XML序列化与反序列化

    public static class XmlHelper { private static void XmlSerializeInternal(Stream stream, object o, En ...