1. alignas (c++11)

设置类和struct的字节对齐方式

默认取值是:  2n : 0, 1, 2, 4 , 6, 8.....

2. alignof

区分sizeof(), alignof得到字节对齐的字节数

#include <iostream>
using namespace std; //对结构体或类进行表示, 设置对齐方式为8字节
struct alignas() S {}; //struct alignas(1) U { S s; } // warning //有字节对齐(以4字节为对齐方式)
struct Foo
{
int i;
float f;
char c;
}; struct Empty {}; struct alignas() Empty64 {}; struct alignas() Double {
double d;
}; //以四字节为对齐方式, 即sizeof(Obj) == 8
struct Obj {
char a;
int b;
}; void alignInfo()
{
cout << "sizeof(Obj) : " << sizeof(Obj) << endl; //
cout << "alignof(Obj) : " << alignof(Obj) << endl; //
cout << "sizeof(Foo) : " << sizeof(Foo) << endl; //
cout << "sizeof(Double) : " << sizeof(Double) << endl; //
cout << "sizeof(Empty64) : " << sizeof(Empty64) << endl; // cout << "\n"; cout << "Alignment of " << endl;
cout << "- char : " << alignof(char) << endl; //
cout << "- pointer : " << alignof(int*) << endl; //
cout << "- class Foo : " << alignof(Foo) << endl; //
cout << "- empty class : " << alignof(Empty) << endl; //
cout << "- alignas(64) Empty: " << alignof(Empty64) << endl; //
cout << "- alignas(1) Double: " << alignof(Double) << endl; //
} int main()
{
alignInfo(); return ;
}

3. auto (c++11)

#include <iostream>
using namespace std; double add(double a, double b)
{
return a + b;
} double get_fun(int a)
{
return a;
} void showAuto()
{
int aa = + ;
auto a = + ;
cout << "type of a: " << typeid(a).name() << endl; auto b = add(, 1.2);
cout << "type of b: " << typeid(b).name() << endl; auto c = {, };
cout << "type of c: " << typeid(c).name() << endl; auto my_lambda = [](int x) { return x + ; };
std::cout << "my_lambda: " << my_lambda() << endl; auto my_fun = get_fun();
cout << "type of my_fun: " << typeid(my_fun).name() << endl;
cout << "my_fun: " << get_fun() << endl; } int main()
{ showAuto();
return ;
}

4. bitand 和 bitor

#include <iostream>
using namespace std; void showBitAndOr()
{
auto a = 3L;
auto b = ; //long
auto c = a bitand b; // &
auto d = a bitor b; // | cout << c << endl; cout << d << endl;
} int main()
{
showBitAndOr(); return ;
}

5. constexpr: 常量表达式(c++11)

  • 用于编译时的常量与常量函数。
  • 声明为constexpr函数的意义是:如果其参数均为合适的编译期常量,则对这个constexpr函数的调用就可用于 期望常量表达式 的场合(如模板的非类型参数,或枚举常量的值)。
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std; int fact(int n)
{
return n < ? : (n * fact(n - ));
} //编译器在编译器时, 就求出来返回的值了
constexpr int factorial(int n)
{
return n <= ? : (n * factorial(n - ));
} template<int N>
struct NN {
void print()
{
cout << N << endl;
}
}; int main(int argc, char *argv[])
{ //在编译器编译时调用的
if (argc > ) factorial(atoi(argv[])); auto aa = fact();
auto bb = factorial();
char group[factorial()]; //编译器在编译时, 就求出来了 NN<factorial()> nn;
nn.print(); return ; }

输出: 6

6. const_cast(避免使用)

7. decltype指定符(c++11)

检查实体的声明类型或表达式的类型及值分类。

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std; struct A {
double x;
A(double t) : x(t) {} }; void testDecltype()
{
A* a = new A(); auto aa = a->x; // aa : double
decltype(a->x) y; // decltype(a->x) : double
decltype((a->x)) z = y; // decltype((a->x)) : double&
z = 23.5;
cout << y << endl; // 23.5 //其他用法
} //c++11, 后置返回类型, 返回值类型由 后面的表达式确定的
//返回值不损失任何精度
template<typename T, typename U>
auto add(T a, U b) -> decltype(a + b)
{
return a + b;
} template<typename T, typename U>
auto add(T a, U b) {
auto c = a + b;
//return c; //返回值
//return (c); //返回引用
return c;
} int main(int argc, char **argv)
{
testDecltype(); return ;
}

8. dynamic_cast转换

沿继承层级向上、向下及侧向转换到类的指针和引用。

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std; struct Base
{
virtual ~Base() {}
}; struct Derived : public Base
{
virtual void name() {}
}; void testDynatic_cast()
{
Base *b1 = new Base();
//拥有基类指针, base指针-->derived指针, 失败
if (Derived *d = dynamic_cast<Derived *> (b1))
{
cout << "donwcast from b1 to d successful\n";
d->name(); // safe to call
} Base *b2 = new Derived();
//成功, 因为b2的确指向derived
if (Derived *d = dynamic_cast<Derived *> (b2))
{
cout << "donwcast from b2 to d successful\n";
d->name(); // safe to call
}
} int main(int argc, char **argv)
{ testDynatic_cast(); return ;
}

9. explicit

struct A
{
A(int) { } // 转换构造函数
A(int, int) { } // 转换构造函数 (C++11)
operator bool() const { return true; }
}; struct B
{
explicit B(int) { }
explicit B(int, int) { }
explicit operator bool() const { return true; }
}; int main()
{
A a1 = ; // OK :复制初始化选择 A::A(int)
A a2(); // OK :直接初始化选择 A::A(int)
A a3 {, }; // OK :直接列表初始化选择 A::A(int, int)
A a4 = {, }; // OK :复制列表初始化选择 A::A(int, int)
A a5 = (A); // OK :显式转型进行 static_cast
if (a1) ; // OK :A::operator bool()
bool na1 = a1; // OK :复制初始化选择 A::operator bool()
bool na2 = static_cast<bool>(a1); // OK :static_cast 进行直接初始化 // B b1 = 1; // 错误:复制初始化不考虑 B::B(int)
B b2(); // OK :直接初始化选择 B::B(int)
B b3 {, }; // OK :直接列表初始化选择 B::B(int, int)
// B b4 = {4, 5}; // 错误:复制列表初始化不考虑 B::B(int,int)
B b5 = (B); // OK :显式转型进行 static_cast
if (b2) ; // OK :B::operator bool()
// bool nb1 = b2; // 错误:复制初始化不考虑 B::operator bool()
bool nb2 = static_cast<bool>(b2); // OK :static_cast 进行直接初始化
}

10. static_assert: 编译时检查

#include <iostream>
using namespace std; template<typename T>
void func(T t)
{
static_assert(alignof(T) == , "only for alignof 4");
} int main()
{
int a = ; func(a); return ;
}

【关键字】c++关键字的更多相关文章

  1. JAVA基础复习与总结<二>构造方法_static关键字_final关键字

    构造方法详解 构造器也叫做构造方法(constructor),用于对象的初始化. class Person2 { String name; int age; public Person2(String ...

  2. oracle: 分割字符串,或者查找字段里面的关键字(关键字1,关键字2,关键字3)

    表中有一个字段:keyword, keyword里面的存储的字符一般是:[关键字1,关键字2,关键字3] 那么,在搜索的时候,不能用like 来模糊查询,因为这样会,多查询出一下不相干的关键字, hi ...

  3. 对象的反序列化流_ObjectInputStream和transient关键字瞬态关键字

    对象的反序列化流_ObjectInputStream package com.yang.Test.ObjectStreamStudy; import java.io.FileInputStream; ...

  4. OC基础--self关键字&super关键字

    PS:OC中的self关键字可以与C#中的this关键字区分记忆,虽然区别还是很大的. OC中的super关键字指的是父类指针 一.self关键字必须了解的知识: 1.使用场合:只能用在方法中(对象方 ...

  5. final关键字+const关键字

    final关键字 1.如果我们希望某个类不被其它的类来继承(可能因为安全考虑),可以使用final. 例题 <? final class A{} class B extends A{};//会报 ...

  6. ref 关键字out关键字

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. 织梦dedecms后台文章搜索关键字,关键字包含文章内容的代码修改

    1.织梦dedecms后台文章搜索功能在哪里找?织梦dedecms后台-->核心-->常用操作-->所有档案列表(或)织梦dedecms后台-->核心-->内容管理--& ...

  8. grep 满足多个关键字 任意关键字 排除多个关键字

    ① grep -E "word1|word2|word3"   file.txt    满足任意条件(word1.word2和word3之一)将匹配. ② grep word1 f ...

  9. static_cast关键字 dynamic_cast关键字

    前言 说起C++中的继承.多态.虚函数等概念,可能很多同学都有所了解,但是要说真正熟知的同学可能就不是很多了.最近在编程过程中了解到C++类型的层次转换(这就涉及到了多态和继承的相关概率),通常C语言 ...

  10. static关键字 weak关键字

    1.static关键字 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart){ ...} 在函数前面加了一个stati ...

随机推荐

  1. Git回滚代码暴力法

    Git回滚有多种方式,这里使用的是[强制提交到远程分支] 效果为:如回滚前的提交记录是 1.2.3.4,使用这种方法回滚到2,那么提交记录就变成了1.2. 操作方法: 需要在本地的Git仓库,右键选择 ...

  2. 【hdu6185】Covering(骨牌覆盖)

    2017ACM/ICPC广西邀请赛-重现赛1004Covering 题意 n*4的格子,用1*2和2*1的砖块覆盖.问方案数(mod 1e9+7).(n不超过1e9) 题解 递推了个式子然后错位相减. ...

  3. Codeforces | CF1029F 【Multicolored Markers】

    这道题其实难度应该小于紫题...除了一点小特判以外没什么难度...\(\leq50\)行代码即可\(AC\)此题 题目大意:给定两个数\(a,b(1\leq a,b\leq 10^{14})\)分别表 ...

  4. 【算法】php计算出丑数

    丑数描述 把只包含因子2,3,5的正整数被称作丑数,比如4,10,12都是丑数,而7,23,111则不是丑数. 判断方法    首先除2,直到不能整除为止,然后除5到不能整除为止,然后除3直到不能整除 ...

  5. 2018蓝桥杯 省赛D题(测试次数)

    x星球的居民脾气不太好,但好在他们生气的时候唯一的异常举动是:摔手机.各大厂商也就纷纷推出各种耐摔型手机.x星球的质监局规定了手机必须经过耐摔测试,并且评定出一个耐摔指数来,之后才允许上市流通.x星球 ...

  6. poj3259Wormholes (Bellman_Ford/SPFA/Floyed算法判断是否存在负环)

    题目链接:http://poj.org/problem?id=3259 题目大意:一个图,有n个顶点,其中有m条边是双向的且权值为为正,w条边是单向的且权值为负,判断途中是否存在负环,如果有输出YES ...

  7. nodejs的某些api~(三)net模块

    net模块和http模块是node最重要的两个模块,前者是基于TCP的封装,后者的本质也是TCP.他们通过 tcp通信,建立一个可以收发消息的web服务器.我在写的作品里面用的express也是封装的 ...

  8. HDU--5519 Sequence II (主席树)

    题目链接 2016年长春ccpc I 题 题目大意 : 给你n(n≤2∗105n≤2∗105)个数,每个数的大小 0<Ai≤2∗10^5   0<Ai≤2∗10^5. 再给你m(m≤2∗1 ...

  9. openvpn部署账号密码登录

    1.开启服务器端路由转发功能: 修改配置文件/etc/sysctl.conf中 net.ipv4.ip_forward = 0 改为 net.ipv4.ip_forward = 1 [root@nod ...

  10. spring+shiro共享session完整小例子

    之前写过一个,只不过那个不单纯,有人跑不通,所以今天整个纯粹的小例子. 要求你有Redis. 源码 GitHub 目录结构 因为这是个例子,仅仅为了体现共享session,所以权限认证部分没有加入处理 ...