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++ 11 游记 1(decltype constexpr)

一. 结缘 decltype

參考source

上code

#include <iostream>

struct A {
double x;
};
const A* a = new A(); decltype( a->x ) x3; // type of x3 is double (declared type)
decltype((a->x)) x4 = x3; // type of x4 is const double& (lvalue expression) template <class T, class U>
auto add(T t, U u) -> decltype(t + u); // return type depends on template parameters int main()
{
int i = 33;
decltype(i) j = i*2; std::cout << "i = " << i << ", "
<< "j = " << j << '\n'; auto f = [](int a, int b) -> int {
return a*b;
}; decltype(f) f2{f}; // the type of a lambda function is unique and unnamed
i = f(2, 2);
j = f2(3, 3); std::cout << "i = " << i << ", "
<< "j = " << j << '\n';
}

初步心得

此论要点:

- 初步掌握四种情形,具体看代码

- 函数后置的返回类型

- 变量(无括号的)申明

- 变量(有括号的)申明

- 和Lamda表达式相关

-

- 须要注意的是。假设一个对象的名称加上括号,它成为左值表达式

  • 他还是lamda表达式的好基友喔。

    decltype is useful when declaring types that are difficult or impossible to declare using standard notation, like lambda-related types or types that depend on template parameters.

尝试难点

If expression is a function call which returns a prvalue of class type or is a comma expression whose right operand is such a function call, a temporary object is not introduced for that prvalue. The class type need not be complete or have an available destructor. This rule doesn’t apply to sub-expressions:in decltype(f(g())), g() must have a complete type, but f() need not.

debug情况

我试着改变lamda表达式中的參数。可是编译器提示我。不能通过,原因待查明

二. constexpr

  • 首先了解字面值 LiteralType,经常使用语字符串

  • 能够在编译时期被动地计算表达式的值

  • constexpr 将编译期常量概念延伸至括用户自己定义常量以及常量函数,其值的不可改动性由编译器保证。因而constexpr 表达式是一般化的。受保证的常量表达式

    比const 前置修饰的函数 的能力 更广

code from csdn

    enum Flags { good=0, fail=1, bad=2, eof=4 };

    constexpr int operator|(Flags f1, Flags f2)
{ return Flags(int(f1)|int(f2)); } void f(Flags x)
{
switch (x) {
case bad: /* … */ break;
case eof: /* … */ break;
case bad|eof: /* … */ break;
default: /* … */ break;
}
} constexpr int x1 = bad|eof; // ok void f(Flags f3)
{
// 错误:由于f3不是常量,所以无法在编译时期计算这个表达式的结果值
constexpr int x2 = bad|f3;
int x3 = bad|f3; // ok。能够在执行时计算
}

code from cpp.com

#include <iostream>
#include <stdexcept> // The C++11 constexpr functions use recursion rather than iteration
// (C++14 constexpr functions may use local variables and loops)
constexpr int factorial(int n)
{
return n <= 1 ? 1 : (n * factorial(n-1));
} // literal class
class conststr {
const char * p;
std::size_t sz;
public:
template<std::size_t N>
constexpr conststr(const char(&a)[N]) : p(a), sz(N-1) {}
// constexpr functions signal errors by throwing exceptions
// in C++11, they must do so from the conditional operator ?:
constexpr char operator[](std::size_t n) const {
return n < sz ? p[n] : throw std::out_of_range("");
}
constexpr std::size_t size() const { return sz; }
}; // C++11 constexpr functions had to put everything in a single return statement
// (C++14 doesn't have that requirement)
constexpr std::size_t countlower(conststr s, std::size_t n = 0,
std::size_t c = 0) {
return n == s.size() ? c :
s[n] >= 'a' && s[n] <= 'z' ? countlower(s, n+1, c+1) :
countlower(s, n+1, c);
} // output function that requires a compile-time constant, for testing
template<int n> struct constN {
constN() { std::cout << n << '\n'; }
}; int main()
{
std::cout << "4! = " ;
constN<factorial(4)> out1; // computed at compile time volatile int k = 8; // disallow optimization using volatile
std::cout << k << "! = " << factorial(k) << '\n'; // computed at run time std::cout << "Number of lowercase letters in \"Hello, world!\" is ";
constN<countlower("Hello, world!")> out2; // implicitly converted to conststr
}

心得

  • 迭代函数的值 在编译期间计算得到!!

    !!

  • 还有就是下图,具体例如以下:

三.其它:好资料

scott meyer 讲cpp11

C++11 FAQ中文版:常量表达式(constexpr)

cpp 我觉得最好资料之中的一个

c++ 11 游记 之 decltype constexpr的更多相关文章

  1. C++11特性:decltype关键字

    decltype简介 我们之前使用的typeid运算符来查询一个变量的类型,这种类型查询在运行时进行.RTTI机制为每一个类型产生一个type_info类型的数据,而typeid查询返回的变量相应ty ...

  2. C++11新特性--decltype (转)

    返回值 decltype(表达式) [返回值的类型是表达式参数的类型] 这个可也用来决定表达式的类型,就像Bjarne暗示的一样,如果我们需要去初始化某种类型的变量,auto是最简单的选择,但是如果我 ...

  3. c++11 auto 与 decltype 详解

    转自: here 一. auto简介 编程时候常常需要把表达式的值付给变量,需要在声明变量的时候清楚的知道变量是什么类型.然而做到这一点并非那么容易(特别是模板中),有时候根本做不到.为了解决这个问题 ...

  4. C++11 类型推导decltype

    我们之前使用的typeid运算符来查询一个变量的类型,这种类型查询在运行时进行.RTTI机制为每一个类型产生一个type_info类型的数据,而typeid查询返回的变量相应type_info数据,通 ...

  5. C++11 auto and decltype

    1.auto关键字 C++新标准引入auto关键词,此auto与之前C语言的auto意义已经不一样了. 这里的auto是修饰未知变量的类型,编译器会通过此变量的初始化自动推导变量的类型. 例如:aut ...

  6. C++ 11 Template ... 与Decltype 测试

    #include <iostream> #include "string" using namespace std; template<typename T> ...

  7. C++11 auto和decltype推导规则

    VS2015下测试: decltype: class Foo {}; int &func_int_r(void) { int i = 0; return i; }; int && ...

  8. C++11新标准:constexpr关键字

    一.constexpr意义 将变量声明为constexpr类型以便由编译器来验证变量是否是一个常量表达式(不会改变,在编译过程中就能得到计算结果的表达式).是一种比const更强的约束,这样可以得到更 ...

  9. C++11常用特性介绍——constexpr变量

    一.constexpr变量 1)将变量声明为constexpr类型以便由编译器来验证变量的值是否为一个常量表达式,声明为constexpr的变量一定是一个常量,而且必须用常量表达式来初始化,如: in ...

随机推荐

  1. 京东SSO单点登陆实现分析

    京东的sso流程: 初始访问状态: cookies: http请求:   1.在首页点击登陆,跳转至passport.360buy.com,给予验证cookie alc(可以试试在提交登陆信息前删除该 ...

  2. [Android 动画]简要分析一下Animator 与 Animation

    大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处(http://blog.csdn.net/kifile),再次感谢 在 A ...

  3. [Asp.net mvc]国际化

    摘要 在实际项目中,经常遇到,开发的项目要提供给不同的国家使用,如果根据国家来开发不同的站点,肯定是非常耗时又耗成本的.asp.net中,提供了一种比较方便的方式,可以使用资源文件的方式,使我们的站点 ...

  4. Spark RDD的fold和aggregate为什么是两个API?为什么不是一个foldLeft?

    欢迎关注我的新博客地址:http://cuipengfei.me/blog/2014/10/31/spark-fold-aggregate-why-not-foldleft/ 大家都知道Scala标准 ...

  5. CentOS 安装 Jenkins

    原文:https://www.sunjianhua.cn/archives/centos-jenkins.html 1.更换源 mv /etc/yum.repos.d/CentOS-Base.repo ...

  6. 根据Request ID找到对应的Session信息

    2018年3月15日 13:04 /* Formatted on 2018/3/15 13:04:45 (QP5 v5.256.13226.35538) */ --根据Request ID找到对应的S ...

  7. Android倒计时功能的实现

    Android中的倒计时的功能(也能够直接使用CountDownTimer这个类直接实现,相关此Demo可查看我的博客).參考了网上写的非常好的一个倒计时Demo: watermark/2/text/ ...

  8. BadgeValueView

    BadgeValueView 效果 源码 https://github.com/YouXianMing/UI-Component-Collection 中的 BadgeValueView // // ...

  9. Android tips(八)-->Android Studio打包apk,aar,jar包

    文本我们将讲解android studio打包apk,aar,jar包的相关知识.apk包就是android系统的安装包,这里没什么好说的,aar包是android中独有的类库包,而jar包是java ...

  10. 删除list中的特定元素

    对于动态删除list中的特定元素,一般用linkedList,删除时有以下两种方法. 1. 循环遍历,找到要删除的元素后删除并且减少list长度.如果不减少list长度,那么就仅仅删除了元素,但没改变 ...