More Related Content Similar to Visual C++で使えるC++11 (20) Visual C++で使えるC++112. 身内向けです。
Visual C++で使えるC++11の機能を、言語機能、
ライブラリに分けて紹介します。
紹介する機能は私が独断と偏見で選んだ一部です。
全て紹介するわけではありません。
コンパイラやライブラリのバグや部分対応までい
くと作業量大変なのでそこら辺は触れません
内容の正しさには気をつけていますが間違えてい
る可能性もあります。
4. Visual C++ 10.0 (Visual Studio
2010)
VC10
Visual C++ 11.0 (Visual Studio
2012)
VC11
Visual C++ 11.0 Community
Technology Preview (Visual Studio
2012)
VC11CTP
Visual C++ 12.0 Preview (Visual
Studio 2013 Preview)
VC12PR
Visual C++ 12.0 (Visual Studio
2013)
VC12
Visual C++ 12.0 Community
Technology Preview (Visual Studio
2013)
VC12CTP
7. 変数の型を右辺式より推論する
修飾子はつかない
C/C++03までの自動変数を表すautoと互換性が
なくなった
template <class F, class G, class T>
void call(const F& f, const G& g, const T& t) {
const auto& temp = f(t);
g(temp);
}
int main() {
auto f = [](int n){return n * n;};
auto g = [](int n){return n + n;};
cout << call(f, g, 10) << endl;
}
10. 式から型を推論する
template <class T, class U>
auto plus(const T& t, const U& u) -> decltype(t + u) {
return t + u;
}
int main() {
vector<double> v;
v.push_back(plus(10, 10.0));
const decltype(v) u = v;
}
12. 従来の参照は正確には左辺値参照
右辺値への参照ができるようになった
機能としてはこれだけだが、本質は別にある。今
回は機能紹介が目的なので省略
int main() {
int a = 10;
int& lval_ref = a;
//int& ng = 10;
int&& rval_ref = 10;
//int&& ng = a;
const int& clval_ref1 = a;
const int& clval_ref2 = 10;
}
13. その場に無名の一時関数オブジェクトを生成する
戻り値の省略可能
変数のコピーキャプチャ、参照キャプチャ可能
int main() {
vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> result;
int border = 5;
copy_if(v.begin(), v.end(), back_inserter(result),
[&border](int it) {
return it > border;
});
}
14. enumの前方宣言が可能になった
要素の整数型を指定できるようになった
要素の最後にカンマが付いても良くなった
enum weekend : unsigned short;
void disp_weekend(weekend w);
enum weekend : unsigned short {
SATURDAY,
SUNDAY,
};
void disp_weekend(weekend w) {
if(w == SATURDAY) {
cout << "SATURDAY" << endl;
} else {
cout << "SUNDAY" << endl;
}
}
int main() { dips_weekend(SATURDAY); }
15. enumにスコープができた
暗黙の型変換を禁止する
enum class traffic_light : unsigned int {
RED,
YELLOW,
BLUE
};
int main() {
traffic_light tl = traffic_light::RED;
unsigned int i = static_cast<unsigned int>(tl);
}
24. 初期化リストに専用の型が出来た。これを引数に
とるコンストラクタを定義することでユーザー定
義型でも初期化リストが使えるようになった。
int sum(initializer_list<int> li) {
return accumulate(li.begin(), li.end(), 0);
}
template <class T, class U>
pair<T, U> create_pair(T t, U u) {
return {t, u}; //どちらかと言うと統一された初期化構文
}
int main() {
auto result = sum({9, 8, 7, 6});
auto p = create_pair("hello", 3.14);
vector<double> v = {1.0, 2.0, 3.0};
}
25. 非静的データメンバを宣言と同時に初期化出来る
C#などでは普通にできる
コンストラクタで初期化すると上書きされる
class person {
public:
person(const string& name, int age) : name_(name), age_(age) {}
int age() const {return age_;}
const string& name() const {return name_;}
private:
string name_ = "noname";
int age = -1;
};
int main() {
person p;
assert(p.age() == -1);
person p2{"tom", 20};
assert(p.name() == "tom");
}
27. 望まない型変換や関数テンプレートのインスタン
ス化を抑止できる
自動生成される関数の生成を抑止できる
template <class T>
void f(){}
//char型だけはダメ
template <>
void f<char>() = delete;
//long longより小さい型はダメ
void g(long long){}
void g(long) = delete;
class hoge {
public:
hoge(const hoge&) = delete;
hoge& operator=(const hoge&) = delete;
//newで割り当てられないようにする
void* operator new(size_t) = delete;
};
33. 関数が例外送出するか有無を指定できる
式が例外を投げるか判定できる演算子
class integer {
public:
integer(int value) : value_(value) {}
int get() const noexcept {return value_;}
private:
int value_;
};
int get(const integer& in) noexcept(noexcept(in.get())) {
return in.get();
}
int main() {
integer n{42};
cout << get(n) << endl;
}
34. alignof 変数や型のアライメントを取得する
alignas 変数宣言の際、アライメントを指定する
int main() {
alignas(int) unsigned char c[32];
alignas(4) int a;
constexpr int int_align = alignof(int);
}
36. C++11 VC++の書き方 バージョン
alignas(x) __declspec(align(x)) VC7
alignof(T) __alignof(T) VC7
thread_local __declspec(thread) VC10
[[noreturn]] __declspec(noreturn) VC7
37. 制限を無くしたunion
属性
char16_t/char32_t
inline namespace
継承コンストラクタ
thread_local
Unicode文字列リテラル
リテラル型に対するconstexpr
62. all_of, any_of, none_of
find_if_not
copy_n, copy_if
move, move_backward
suffle
is_partitioned
partition_copy, partition_point
is_sorted, is_sorted_until
is_heap, is_heap_until
minmax, minmax_element
is_permutation
65. function, bad_function_call
bind, is_bindexpression, is_placeholder,
placeholders::_1, placeholders_2,
placeholders::_N, mem_fn
reference_wrapper, ref, cref
hash
71. deque
◦ cbegin, cende, crbegin, crend
◦ shrink_to_fit
◦ emplace, emplace_back, emplace_front
72. list
◦ cbegin, cende, crbegin, crend
◦ emplace, emplace_back, emplace_front
73. set
◦ cbegin, cende, crbegin, crend
◦ emplace, emplace_hint
multi_set
◦ cbegin, cende, crbegin, crend
◦ emplace, emplace_hint
74. map
◦ cbegin, cende, crbegin, crend
◦ at
◦ emplace, emplace_hint
multi_map
◦ cbegin, cende, crbegin, crend
◦ at
◦ emplace, emplace_hint
75. queue
◦ emplace, swap
◦ reference, const_reference
priority_queue
◦ emplace, swap
◦ reference, const_reference
swap
78. basic_string
◦ cbegin, cende, crbegin, crend
◦ shrink_to_fit
◦ pop_back, back, front
stoi, stol, stoul, stoll, stoull, stof, stod, stold
82. unitilialized_copy_n
shared_ptr, unique_ptr, weak_ptr,
enable_shared_from_this, bad_weak_ptr,
owner_less, default_delete, addresso
make_shared, allocate_shared,
static_pointer_cast, dynamic_pointer_cast,
const_pointer_cast
pointer_safety, get_pointer_safety,
declare_reachable, undeclare_reachable,
declare_no_reachable, undeclare_no_reachable
84. forward, move(VC10)
tuple_size, tuple_element, get(VC10)
move_if_noexcept(VC11)
declval, piecewise_constrcut_t,
piecewise_constrcut(VC11)
87. HUGE_VALF, HUGE_VALL, INFINITY, NAN
FP_INFINITE, FP_NAN, FP_NORMAL,
FP_SUBNORMAL, FP_ZERO
FP_FAST_FMA, FP_FAST_FMAF, FP_FAST_FMAL
FP_ILOGB0, FP_ILOGBNAN
MATH_ERRNO, MATH_ERREXCEPT
math_errhandling
90. <cmath>
◦ asinh, acosh, atanh
◦ exp2, expm1, log1p, log2
◦ ilogb, logb, scalbn, scalbln
◦ cbrt, hypot
◦ erf, erfc, tgamma, lgamma
◦ trunc, round, nearbyint, rint, lrint, llrint
◦ remainder, remquo
◦ copysign, nan, nextafter, nexttoward
◦ fmax, fmin, fdim, fma
◦ fpclassify, isfinite, isinf, isnan, isnormal, signbit
◦ float_t, double_t,
※グローバル領域には存在する。std名前空間に所属
していない