[c++]c++11新規格

1393 ワード

1. constexpr
変数が定数式であることをコンパイラに確認するように要求
constexpr int sz=size(); //     size() constexpr function,     

constexprでポインタを修飾すると、ポインタ定数であり、定数を指すポインタではないことを示します.
const int *p=nullptr; //       
constexpr int *q=nullptr;//    

2. alias declaration
using SI=Sales_item;// similar to typedef  Sales_item SI;

3. auto 
コンパイラは、式の結果に基づいて変数タイプを自動的に推定します.
Autotop-level const,low-level const保持を無視
int a=10;
const int * const p=&a;
auto pp=p;// pp is const int *         ,top-level const   auto     const
const auto pp=p;

4. decltype
このタイプ(like auto)を初期化するのではなく、コンパイラで式から推測したタイプだけを使用したい場合はdecltypeを使用します.
decltype(f()) sum=x;

decltypeはtop-levelとlow-levelのconstを同時に保持している
5.クラス内のイニシエータ
{}または=,()だめです.
class book{
string isbn;
double price{0.0};
int piece=0;
};

6. range for statement
for(declaration : expression)
    statement;
//example
string str("some string");
decltype(str.size()) count=0;
for(auto c: str)
    if(isspace())
    count++;

//if need to change the element in str using auto &
for(auto &c:str)
    c=toupper(c);

7.vectorをカッコで初期化
vector strs={"erd","sdg","acv"};//もう一つpush_back()だ
かっこは()で代用できません