[c++] constexpr and literal class

稀奇古怪的新特性,菜鸟在此啄上一啄。

1.

When should literal classes be used in C++?

2.

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修饰的变量是可以表示数组大小的。

放在 stack上的数组也是可以的,比如main中的如下:

int main()
{
   int i;
   cin >> i;
   int arr[i];
}

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

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

3.

Want speed? Use constexpr meta-programming!

static的方式是最快的。

4.

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

Suppose it does something a little more complicated.

constexpr int MeaningOfLife ( int a, int b ) { return a * b; }
const int meaningOfLife = MeaningOfLife( 6, 7 );

使用组合!

Now you have something that can be evaluated down to a constant while maintaining good readability and allowing slightly more complex processing than just setting a constant to a number.

It basically provides a good aid to maintainability as it becomes more obvious what you are doing. Take max( a, b ) for example:

template< typename Type > constexpr Type max( Type a, Type b ) { return a < b ? b : a; }

替代模板计算的一个很好的方式!效率比模板高。


Literal Type

down voteaccepted

The defintion of "literal type" is in [basic.types] (C++11 3.9):

A type is a literal type if it is:

  • void; or
  • a scalar type; or
  • a reference type; or
  • an array of literal type; or
  • a class type (Clause 9) that has all of the following properties:
  • it has a trivial destructor,
  • it is an aggregate type (8.5.1) or has at least one constexpr constructor or constructor template that is not a copy or move constructor, and
  • all of its non-static data members and base classes are of non-volatile literal types.

Literal types are broadly speaking the types that are permitted to occur as and in constant expressions.

There is a trait, std::is_literal_type, for checking if a type is a 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 0;
}

Output:

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


一些问题:

constexpr vs template for compile-time maths functions?

元编程

泛型编程

continue...

时间: 2024-08-28 12:32:31

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

c++ 11 游记 之 decltype constexpr

title: c++ 11 游记 1 keyword :c++ 11 decltype constexpr 作者:titer1 zhangyu 出处:www.drysaltery.com 联系:1307316一九六八(仅接受短信) 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处. tips for image: http://7xjs3n.com1.z0.glb.clouddn.com c+

【C++】constexpr和常量表达式

常量表达式(const expression)是指值不会改变并且在编译过程就能得到计算结果的表达式.显然,字面值属于常量表达式,用常量表达式初始化的const对象也是常量表达式.后面将提到,C++语言有几种情况下是要用到常量表达式的. 一个对象(或者表达式)是不是常量表达式由它的数据类型和初始值共同决定,例如: const int max_files = 20; //max_files是常量表达式 const int limit = max_files + 1; //limit是常量表达式 in

Literal 字面值 字面量 的理解

Literal 字面值 字面量 Literal, 在程序语言中,指表示某种数据值的符码.如,123 是整数值符码, 3.14 是浮点值符码,abcd 是字串值符码,True, False, 是逻辑值符码,等. Literal 代表的是数据值的本身,区别于“变量”(Variable, 即容纳数据值的 内存址).最直观的理解是,碰到 literal, 你不会想到要往里面写东西:如:123 = "Hello, World!", 象变量 那样.换句话说,Literal 是一个常量(Consta

C++总结:C++中的const和constexpr

C++中的const可用于修饰变量.函数,且在不同的地方有着不同的含义,现总结如下. const的语义 C++中的const的目的是通过编译器来保证对象的常量性,强制编译器将所有可能违背const对象的常量性的操作都视为error. 对象的常量性可以分为两种:物理常量性(即每个bit都不可改变)和逻辑常量性(即对象的表现保持不变).C++中采用的是物理常量性,例如下面的例子: 1 2 3 4 5 6 7 struct A {     int *ptr; }; int k = 5, r = 6;

Smarty中{literal}的使用详解

 {literal} <script>function Login(){ document.LoginForm.submit();}</script>{/literal} ===============================================================================Literal 标签区域内的数据将被当作文本处理,此时模板将忽略其内部的所有字符信息. 该特性用于显示有可能包含大括号等字符信息的 javascript 脚

解决sqoop 导入oracle表时 --split-by参数为日期类型时的报错:ORA-01861: literal does not match format string

报错栈: 2017-06-08 18:46:13,422 INFO [main] org.apache.sqoop.mapreduce.db.DBRecordReader: Executing query: select "JFRQ","ZYH","FYKS","KSSE","YBJE","YPJE","ZJJE" from BSHIS."DEPCS_T_D

asp.net中Literal与label的区别

摘自:http://www.cnblogs.com/Fskjb/archive/2010/03/19/1690233.html 一.方案 Literal 最常用于向页面中动态添加内容. 二.背景 Literal 控件表示用于向页面添加内容的几个选项之一.对于静态内容,无需使用容器,可以将标记作为 HTML 直接添加到页面中.但是,如果要动态添加内容,则必须将内容添加到容器中.典型的容器有 Label 控件.Literal 控件.Panel 控件和 PlaceHolder 控件. Literal

[Error]EOL while scanning string literal

有一个经常性的工作项目.需要一天的一些表数据到外部接口,但最近总是异常.今天检查的原因. 第一本地和测试环境中测试程序是没有问题,有网络环境只会在日志中抛出一个异常.产生主要的例外是推定异常数据. ,由不得而知,于是添加了程序的输出日志和数据打印,通过几次的执行定位了错误发生的函数和数据记录. 异常是这种: EOL while scanning string literal: <string>, line 1, pos 7 google一下.这个异常造成的原因是字符串,引號没有成对出现,也就是

constexpr的用法

我的观点:今天有幸看到各位大神们在讨论constexpr的前途,有人说vs2010.2011.2012.2013都不支持,所以就觉得没用.好吧,我的世界中vs并不是不可获取,好吧,自己为了口头的胜利开始胡扯了.constexpr就像是一个告诉编译器,this is a const XXtype.但是各种有趣的实验证明了这个玩意还是有用的. 我来给你们验证一下: 1.如数一个数字,计算出它的!的结果(忘记叫什么了eg:5!==5*4*3*2*1==120) 1 //build-1234-Deskt