Skip to content

Instantly share code, notes, and snippets.

@plasma-effect
Last active January 6, 2016 09:45
Show Gist options
  • Save plasma-effect/4d6ff2f0edec0ce87c9c to your computer and use it in GitHub Desktop.
Save plasma-effect/4d6ff2f0edec0ce87c9c to your computer and use it in GitHub Desktop.
VC++ in VS2015

ブログに゜ヌスコヌド貌っ぀けるよりgistにマヌクダりンで曞いたほうが圧倒的に楜なこずに気づいた。

2015幎7月20日にVisual Studio 2015がリリヌスされた。圓然VC++も新しくなったわけだがそのうち新しく远加された機胜を出来る限り毎日蚘事にしようず思う。 本日はそのpart01、constexprだ。

constexprはC++11で远加された機胜で、珟圚のC++14では曎にパワヌアップしおいるのだが、Microsoftの脳みそが2011幎から進んでないのでC++11版のconstexprを玹介する。

コンパむル時においお定数匏はその堎で蚈算される。定数匏ずは䟋えば以䞋の様なもの。

int main(){
  const int v = 1 + 2;//明らかに3
  const int u = 3 * 5;//明らかに15
}

䞀方でコンパむル時に䜿える定数(以䞋コンパむル時定数)が存圚する。定数匏で初期化されおいるconst定数がそれ。䞊の䟋で蚀えばvもuもコンパむル時定数である。コンパむル時に䜿えるずは䟋えばtemplate匕数に枡せる。

template<int N>struct test{
  static int get(){
    return N;
  }
};
 
int main(){
  const int v = 1 + 2;
  const int u = test<v>::get();
}

この䟋ではvはコンパむル時定数なのでtemplateクラスtestの匕数に枡せる。䞀方でstaticメンバ関数getは定数匏ではないのでuはコンパむル時定数でない定数である。この堎合uはtemplate匕数に枡せない。

倉数に察するconstexprは倉数がコンパむル時定数であるこずを保蚌する指定子である。型はあくたでconstであり、constexprではない。

int hoge(){
  return 0;
}
 
int main(){
  constexpr int v = 0;//OK
  constexpr int u = hoge();//NG
}

䞀方で関数に察するconstexprはその関数を定数匏ずしお扱えるようにする指定子である。constexpr関数の䞭身は実質的にreturn文䞀぀でなければならない。

constexpr int hoge(){
  return 0;
}
constexpr int fact(int n){
  return n==0 ? 1 : n*fact(n-1);
}
 
int main(){
  constexpr int v = 0;//OK
  constexpr int u = hoge();//OK
  constexpr int w = fact(5);//OK
}

return文䞀぀ずいっおも条件挔算子が䜿えるので関数型蚀語に慣れおいる人なら芪しみを持おるのではないだろうか。

定数匏ずできるのは組み蟌み型がたず䞊げられるがナヌザヌ定矩でconstexpr定数、たたはconstexpr関数に䜿えるクラスも䜜るこずができる。定数匏に䜿える型をリテラル型ず蚀う。

リテラル型には組み蟌み型やその配列がたず考えられるが、それらを組み合わせたナヌザヌ定矩型でリテラル型ずなる条件は以䞋のずおり。
・デストラクタがトリビアル
・次のどちらかを満たす「aggregateである」「コピヌでもムヌブでもないconstexprコンストラクタ、たたはtemplateなコンストラクタを持぀」
・staticでないメンバ倉数が党おvolatileでもないリテラル型
䟋えば以䞋の様なクラスがリテラル型ずなる。

struct pair{
  int x_,y_;
  constexpr pair(int x=0,int y=0):x_(x),y_(y){}
  pair(pair const&)=default;
  pair(pair&&)=default;
  ~pair()=default;
};
 
int main(){
  constexpr pair p(0,1);
  constexpr int u = p.x_;
  constexpr int v = p.y_;
}

そういえば䞊のコヌドでわかるようにVS2015ではムヌブコンストラクタもdefault宣蚀できるようになった。むしろ今たでできなかったほうがおかしい。

リテラル型やconstexprに関する掻甚法はここでは曞かない。様々な堎所で論じられおいるのでそちらを参考にしお欲しい。

参考ペヌゞ 䞭女子でもわかる constexprhttp://www.slideshare.net/GenyaMurakami/constexpr-10458089
リテラル型クラスの条件、および「䞭女子でもわかる constexpr」の蚂正http://boleros.hateblo.jp/entry/20130718/1374155184(http://boleros.hateblo.jp/entry/20130718/1374155184)
C++11の文法ず機胜(C++11: Syntax and Feature)http://ezoeryou.github.io/cpp-book/C++11-Syntax-and-Feature.xhtml#basic.types(http://ezoeryou.github.io/cpp-book/C++11-Syntax-and-Feature.xhtml#basic.types)

䜙談
VC++のconstexprに぀いおの各皮怜蚌しおない。誰か頌みたす(他人任せ)。

「VS2015の蚘事を芋おたはずだがい぀の間にかconstexprの蚘事になっおいた」ずいう意芋が出た。VC++固有の機胜の蚘事なんお俺が曞けるわけ無いだろ

2015幎7月20日にVisual Studio 2015がリリヌスされた。圓然VC++も新しくなったわけだがそのうち新しく远加された機胜を出来る限り毎日蚘事にしようず思う。 本日はそのpart02、ナヌザヌ定矩リテラルだ。

ある型の䞀時倉数を䜜るのにわざわざ関数を䜿いたくはないずいう時があったりなかったりする。䟋えば耇玠数倀型など。

struct complex{
  int re_,im_;
  constexpr complex(int re=0, int im=0):re_(re),im_(im){}
  complex(complex const&)=default;
  complex(complex&&)=default;
  ~complex()=default;
};
 
int main(){
  complex v = complex(0,2);//2iっお曞きたい
}

100uでunsigned int型の100を衚すようにナヌザヌ定矩のリテラルを䜿いたい。 operator""でそのようなナヌザヌ定矩のリテラルを䜜るこずができる。

struct complex{
  int re_,im_;
  constexpr complex(int re=0, int im=0):re_(re),im_(im){}
  complex(complex const&)=default;
  complex(complex&&)=default;
  ~complex()=default;
};
 
complex operator"" _i(unsigned long long int v){
  return complex(0, v);
}
 
int main(){
  complex v = 2_i;//complex(0, 2);
}

ナヌザヌ定矩リテラルにはいく぀か成玄がある。たず匕数の型にはunsigned long long intやlong doubleずいった䞀郚の型しか䜿えない。

int operator"" _hoge(unsigned long long int v){//OK
  return static_cast<int>(v);
}
int operator"" _piyo(int v){//NG
  return static_cast<int>(v);
}
 
int main(){}

たた_(アンダヌバヌ)から始たらないリテラルは予玄されおるので䜿うず色んな人に怒られる。コンパむラにも怒られる。

int operator"" hoge(unsigned long long int v){
  return static_cast<int>(v);
}//コンパむルは通るが譊告が出る
 
int main(){}

あず芏栌的には""ず_の間に空癜文字(スペヌスなど)が必芁 らしい。別にMSVCでもgccでもclangでもスペヌス入れなくおも通るんだから気にする必芁ずかないんじゃないのかこれ

unsigned long long intの他に䜿える型を玹介する。たずはlong double

int operator"" _to_int(long double d){
  return static_cast<int>(d);
}
 
int main(){
  int v = 2.0_to_int;
}

char const*

#include<utility>
int operator"" _hoge(char const*, std::size_t s){
  return s;
}
 
int main(){
  int v = "test"_hoge;
}

あずはwchar_tずかそこら蟺である。

ナヌザヌ定矩リテラルでは基本的にtemplateが䜿えない。ただしvariadic templateを䜿甚できる唯䞀の䟋が存圚する。

template<char... Cs>struct string_t{};
template<char... Cs>string_t<Cs...> operator"" _hage(){
  return string_t<Cs...>{};
}
 
int main(){
  auto x = 12345_hage;//string_t<'1', '2', '3', '4', '5'>
}

この堎合匕数は付けない。たたunsigned long long intのみに察応する。

参考
本の虫 ナヌザヌ定矩リテラルのすべおhttp://cpplover.blogspot.jp/2012/02/blog-post_16.html
C++11の文法ず機胜(C++11: Syntax and Feature)http://ezoeryou.github.io/cpp-book/C++11-Syntax-and-Feature.xhtml#over.literal

䜙談
今埌こっちに蚘事を曞いおいこうず思いたす。ブログずはなんだったのか。

俺が蚀いたいのは「VS2015は䟿利だから䜿おうね」ではない。「2015幎にVC++2005ずかVC++2008ずか䜿うな」である。最䜎限VS2013以降にしおくれ。

2015幎7月20日にVisual Studio 2015がリリヌスされた。圓然VC++も新しくなったわけだがそのうち新しく远加された機胜を出来る限り毎日蚘事にしようず思う。 本日はそのpart03、返り倀autoだ。

templateを䜿っおるずたたに関数の返り倀を指定したくない時がある。極端な䟋だが次のクラスなど。

struct add_t{
	template<class Lhs,class Rhs>Lhs operator()(Lhs lhs, Rhs rhs)const{
		return lhs + rhs;
	}
};

int main(){
	add_t add{};
	auto v = add(1, 2.0);//vはint型
}

int型ずdouble型のoperator+の返り倀はdouble型に決たっおいるのだがこの堎合add_t::operator()の返り倀がLhsに䟝存しおいるためint型になっおしたっおいる。今たでも返り倀の埌眮蚘法ずdecltypeで察凊はできたのだが

struct add_t{
	template<class Lhs,class Rhs>auto operator()(Lhs lhs, Rhs rhs)->decltype(lhs + rhs) const{
		return lhs + rhs;
	}
};

int main(){
	add_t add{};
	auto v = add(1, 2.0);//vはdouble型
}

同じ匏を2回曞いおいる。片方を曞き換えた時もう片方を曞き換え忘れたら悲劇が起きる。

これからは単玔にautoだけでよい。するずreturn文の䞭身から型を掚論しおくれる。

struct add_t{
	template<class Lhs,class Rhs>auto operator()(Lhs lhs, Rhs rhs)const{
		return lhs + rhs;
	}
};

int main(){
	add_t add{};
	auto v = add(1, 2.0);//vはdouble型
}

䞊の䟋のように、特にtemplateを䜿うずきにこの機胜は真䟡を発揮する。

たるで銀の匟䞞のようなこの機胜だが、圓然銀の匟䞞ではない。原理ずしおは倉数に察するautoず同じなので色々成玄がある。たずそのたただず参照でない。

auto hoge(int& x){//int&->int
	return x;
}
auto&& piyo(int& x){//int&->int&
	return x;
}
int main(){
	int x{10};
	hoge(x)=5;//NG
	piyo(x)=5;//OK
}

たた耇数のreturn文が存圚するずきはそれらは党お同じ型でなくおはならない。実甚䞊では最初に珟れたreturn文の型ず盞反するreturn文が存圚しおはいけない。

auto hoge(int x){
	if(x > 0)
		return true;
	else
		return false;
}

auto piyo(int x){
	if(x > 0)
		return true;
	else
		return 0;//NG
}

逆に蚀えば最初のreturn文で䞀応返り倀の型は掚論されおいるわけである。぀たりconstexprでなければ再垰が曞ける。

auto fact(int n){
	if(n == 0)
		return 1;
	else
		return n * fact(n-1);//OK、この段階でfactはint->intず掚論されおいる
}

int main(){}

なぜconstexprだず曞けないのかずいうずconstexprではreturn文が䞀぀なのでその段階では返り倀の型がわかっおない(=return文に珟れおはいけない)ため。

あず返り倀autoを倚甚するずぱっずコヌドを芋た時返り倀の型がわからなくなる。VS2015を前提ずしおるのでむンテリセンスがあるのだが非template関数のintやdoubleはそのたた曞いたほうがいいような気もする。

参考
巻末: C++14の新機胜http://ezoeryou.github.io/cpp14-appendix/#return_type_deduction

䜙談
生ポむンタは危ないからスマヌトポむンタを䜿おうね(boost䜿うずコンパむル時間がブヌストするからVS2013以降を䜿えの意)。

さらに䜙談
投皿したあずに気づいたのだが4぀目のコヌド(の9行目をコメントアりトしたもの)がむンテリセンスでは匕っかかるのにコンパむルは通るコヌドになっおいる。

オヌバヌロヌドされた関数の関数ポむンタの取り方ずか぀いさっき知った。

2015幎7月20日にVisual Studio 2015がリリヌスされた。圓然VC++も新しくなったわけだがそのうち新しく远加された機胜を出来る限り毎日蚘事にしようず思う。 本日はそのpart04、ゞェネリックラムダず汎甚ラムダキャプチャヌだ。

今たでラムダは匕数の型は指定しなくおはならなかった。

#include<algorithm>

int main()
{
	int X[] = { 0,1,2,3,4 };
	int x = std::count_if(std::begin(X), std::end(X), [](int v) {return v % 2 == 0;});
}

基本的にそれで別にいいのだがtemplateを䜿う時などに柔軟に察凊したい時がある。 䟋えばC++においおはオヌバヌロヌドされた関数の関数ポむンタを取るには単玔な手段は取れない(参考Cry’s Diary オヌバヌロヌドされた関数ぞのポむンタ / 関数テンプレヌトぞのポむンタ)。 STLなどで䜿う堎合は関数ポむンタでなくおもよい(関数オブゞェクトであれば枡せる)が

#include<utility>
namespace detail {
	int twice(int n) {
		return 2 * n;
	}

	double twice(double d) {
		return 2.0 * d;
	}
}

struct {
	template<class T>auto operator()(T v)const {
		return detail::twice(v);
	}
}const twice;

template<class Func, class... Ts>auto function_call(Func func, Ts&&... args) {
	return func(std::forward<Ts>(args)...);
}

int main(){
	auto v = function_call(twice, 2);//OK
	auto u = function_call(twice, 1.0);//OK
}

クラスを自䜜する等のたどろっこしい手順を取らなければならない。ラムダでもtemplateのようなものを䜿いたい。

これからはautoを䜿うこずでそのようなこずができる。

#include<utility>
namespace detail {
	int twice(int n) {
		return 2 * n;
	}

	double twice(double d) {
		return 2.0 * d;
	}
}
template<class Func, class... Ts>auto function_call(Func func, Ts&&... args) {
	return func(std::forward<Ts>(args)...);
}

int main(){
	auto twice = [](auto v) {return detail::twice(v);};
	auto v = function_call(twice, 2);//OK
	auto u = function_call(twice, 1.0);//OK
}

圓然耇数の匕数でautoを曞けばそれぞれでゞェネリックになる。

int main() {
	auto add = [](auto x, auto y) {
		return x + y;
	};
	auto v = add(1, 2.0);//OK
}

今たでラムダ匏のキャプチャヌはコピヌキャプチャヌず参照キャプチャヌの2぀しかなかった。

int main(){
	int x{};
	auto func = [=]{//コピヌキャプチャヌ
		++x;//NG、xは倉曎䞍胜
	};
	func();
	
	auto func2 = [&]{//参照キャプチャヌ
		++x;//OK、xは1になる
	};
	func2();

	auto func3 = [=]()mutable{//mutableなコピヌキャプチャヌ
		++x;//OK、ただし元のxに倉化はない
	};
	func3();
	
	auto func4 = [&x]{//xのみを参照キャプチャヌ
		++x;//OK、xは2になる
	};
	func4();
}

これはクラスのメンバをキャプチャヌするずきに問題がある。クラスのメンバ関数内でのラムダ匏のキャプチャヌはthisずロヌカル倉数のみをキャプチャヌしおいる。

struct test{
	int x;
	auto get_closure(){
		return [=]{
			return x;
		};
	}
	auto get_closure2(){//closureは次ず同じ
		return [this]{
			return this->x;
		};
	}
};

int main(){}

この時thisの寿呜によっお問題が発生したりする。具䜓的にはthisを指す倉数が定矩されたスコヌプを抜けたあずに呌びだそうずした堎合は未定矩動䜜ずなる。

新しいラムダキャプチャヌでは倉数宣蚀かのように指定できる。

struct test{
	int x;
	auto get_closure(){
		return [x = x]{
			return x;
		};
	}
};

int main(){
	int m=0;
	auto func = [z = m + 2]{
		return z;
	};
}

std::moveや参照なども圓然䜿える。

参考
1.3 汎甚lambdaキャプチャヌ(Generalized Lambda-capture)http://ezoeryou.github.io/cpp14-appendix/#init_captures
1.4 ゞェネリックlambdahttp://ezoeryou.github.io/cpp14-appendix/#generic_lambda

䜙談
最新のC++の機胜を䜿い尜くしおコヌド曞いたゲヌムずか䞀回ぐらい䜜っおみたいねぇ

曞くネタが尜きおきた。ここずにらめっこしながら曞いおいる。

2015幎7月20日にVisual Studio 2015がリリヌスされた。圓然VC++も新しくなったわけだがそのうち新しく远加された機胜を出来る限り毎日蚘事にしようず思う。 本日はそのpart05、constexpr化した暙準ラむブラリだ。

䞀郚の暙準ラむブラリはpart01で玹介したconstexprに察応した。今回はそれを玹介しおいく。

たずはcomplex

#include<complex>

int main() {
	constexpr std::complex<int> x{ 1,2 };
	constexpr std::complex<int> y{ 2,2 };

	constexpr bool a = x == y;
	constexpr bool b = x != y;
	constexpr bool c = x.real() == y.real();
	constexpr bool d = x.imag() == y.imag();
}

他にもTy_ずcomplexのoperator==などがconstexprされおいる。䞀方でoperator+などはconstexpr化されおいない。泚意。

次にarray

#include<array>

int main() {
	constexpr std::array<int, 4> v{ 1,2,3,4 };
	
	constexpr int v0 = v[0];
	constexpr int v1 = v[1];
	constexpr int v2 = v[2];
	constexpr int v3 = v[3];
}

元からAggregateだし今曎感はある。

chrono

#include<chrono>

int main() {
	constexpr std::chrono::milliseconds sen{ 1000 };
	constexpr std::chrono::milliseconds hyaku{ 100 };
	
	constexpr auto senhyaku = sen + hyaku;
	constexpr auto kyuhyaku = sen - hyaku;
}

他にはoperator==などもconstexprになっおいる。

initializer_list

#include<initializer_list>

int main() {
	constexpr std::initializer_list<int> lis{};
	
	constexpr auto b = lis.begin();
	constexpr auto e = lis.end();
	constexpr auto s = lis.size();
}

デフォルトコンストラクタがconstexprになった。

utility

#include<utility>

int main() {
	constexpr int v{ 10 };
	constexpr int u = std::forward<int const&>(v);

	constexpr std::pair<int, double> p{ 1,2.0 };
	constexpr auto a = p.first;
	constexpr auto b = p.second;
	constexpr auto p2 = std::make_pair(1, 2.0);
	constexpr bool x = p == p2;
}

他にはstd::move、std::move_if_noexceptもconstexprに。

tuple

#include<tuple>

int main() {
	constexpr std::tuple<int, double, short> t{ 1,2.0,3 };
	constexpr auto x = std::get<0>(t);
	constexpr auto y = std::get<1>(t);
	constexpr auto z = std::get<2>(t);
}

functional

#include<functional>

int main() {
	constexpr std::plus<> plus{};
	constexpr auto x = plus(1, 2);
}

参考
N3302http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3302.html
N3469http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3469.html
N3470http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3470.html
N3471http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3471.html
N3789http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3789.htm

䜙談
䞊列凊理関連がさっぱりなので今回のシリヌズでは觊れない予定

VC++のstd::make_index_sequenceを再垰深床線圢オヌダヌで実装したや぀出おこい、説教しおやる。

2015幎7月20日にVisual Studio 2015がリリヌスされた。圓然VC++も新しくなったわけだがそのうち新しく远加された機胜を出来る限り毎日蚘事にしようず思う。 本日はそのpart06、noexceptだ。

か぀おC++の芏栌には動的䟋倖指定ずいう機胜があった。いや今でもあるのだが今ではdeprecatedである。倖に投げうる䟋倖の型を指定する機胜だ。

int add(int x, int y)throw() {//䟋倖は蚱されない
	return x + y;
}

class div_zero_exception{
public:
	div_zero_exception(){}
};

int div(int x, int y)throw(div_zero_exception) {//div_zero_exceptionのみが蚱される
	if (y == 0)throw div_zero_exception();
	return x / y;
}

int main(){}

これをMSVCでコンパむルしたずきの譊告を芋ればわかるだろうがMSVCではthrowの䞭身なんお空か吊かしか芋おいない。次のコヌドを実行すればそのこずがわかる。

#include<iostream>
namespace test {//divがグロヌバル名前空間(おそらくC由来の暙準ラむブラリ)で衝突したので
	class div_zero_exception {
	public:
		div_zero_exception() {}
	};

	int div(int x, int y)throw(div_zero_exception) {//div_zero_exceptionのみが蚱されるはずだった
		if (y == 0)throw 1;
		return x / y;
	}
}

int main(){
	try {
		test::div(1, 0);
	}
	catch(int){
		std::cout << "test" << std::endl;
	}
}

芏栌ではtest::divを呌び出したずきにstd::terminateが呌ばれるこずになっおいる。gccやclangでは埋儀にstd::terminateが呌ばれる。だが倚くの実装で動的䟋倖指定はなかったこずされおいたらしい。

䞀方throw()の時はその関数は䟋倖を倖に出さない、蚀い換えれば䟋倖が発生したらその堎でstd::terminateが呌ばれるこずが保蚌される。芁するにこの䜿い方に関しおはずおも䟿利だったずいうこずだ。

そしおそれ専甚の文法が远加された、それがnoexceptである。

#include<type_traits>
void test()noexcept{}//䟋倖を投げないこずを保蚌
void test2()noexcept(false){}//䟋倖を投げるかもしれない
void test3()noexcept(true){}//䟋倖を投げないこずを保蚌
template<class T>void test4(T)noexcept(std::is_same<int, T>::value){}//Tがintなら投げないこずを保蚌、そうでない堎合投げるかもしれない

noexceptだけなら䟋倖を投げないこずを保蚌し、noexcept(定数匏)なら定数匏を評䟡しおtrueになら投げない、falseなら投げうる。 これのいいずころは䞊でもわかるようにtemplate匕数によっお容易に指定を倉えるこずができる点だ。圓然メタプログラミングの技術を䜿えばより耇雑な動きができる。

なお、noexcept指定は倖に䟋倖を投げないこずを指定するだけなので内郚で䟋倖を発生させおtry-catchで察凊する堎合はなんら問題ない。

void test()noexcept{
	try{
		throw 1;
	}
	catch(int){}
}

int main(){
	test();
}

さらに挔算子ずしおnoexceptも远加された。匏を枡しお䟋倖を投げうる匏を含たないならtrue、含むならfalseを返す。

void test()noexcept(false){}
void test2()noexcept{}
void test3(){}

int main() {
	static_assert(noexcept(1 + 2), "1 + 2");//明らかに䟋倖を投げない
	static_assert(noexcept(throw 1), "throw 1");//明らかに䟋倖を投げおいる
	static_assert(noexcept(test()), "test()");//noexcept(false)が指定されおいる
	static_assert(noexcept(test2()), "test2()");//noexceptが指定されおいる
	static_assert(noexcept(test3()), "test3()");//䜕も指定されおないため投げられるかもしれない
	static_assert(noexcept(noexcept(test())), "noexcept(noexcept(test()))");//noexcept挔算子自䜓は䟋倖を投げない
}

䞊の䟋でわかるようにnoexceptは定数匏であり、したがっお䟋倖を投げない。たたnoexceptの䞭身は評䟡されない。 これで匕数ずしお枡された関数オブゞェクトが䟋倖を投げうるかどうかで凊理を倉えるこずもできる。

参考
C++11の文法ず機胜(C++11: Syntax and Feature)noexcept挔算子http://ezoeryou.github.io/cpp-book/C++11-Syntax-and-Feature.xhtml#expr.unary.noexcept
C++11の文法ず機胜(C++11: Syntax and Feature)䟋倖指定http://ezoeryou.github.io/cpp-book/C++11-Syntax-and-Feature.xhtml#except-spec

䜙談
noexcept指定した関数内で䟋倖が発生した堎合の挙動だが、VS2015の堎合デストラクタが呌ばれる保蚌がないそうだ(https://msdn.microsoft.com/en-us/library/dn956976.aspx参照)。

そういやサンプルコヌドのラむセンス考えるの忘れおた。自由に䜿っおいいし自由に改倉しおいいし自由に転茉しおいい。

2015幎7月20日にVisual Studio 2015がリリヌスされた。圓然VC++も新しくなったわけだがそのうち新しく远加された機胜を毎日玹介しおきた。 本日は最終回、ただ玹介しおない機胜を少しず぀玹介しおいく。

通垞、文字列リテラルは""で囲む。この堎合実装䟝存の゚ンコヌドになる。
これをUTF-8、UTF-16、UTF-32に指定したい堎合はそれぞれu8""、u""、U""で囲む。
たたUTF-16、UTF-32甚にchar16_tずchar32_tずいう型が远加された。

int main() {
	char const str[] = u8"1234";
	char16_t const str2[] = u"1234";
	char32_t const str3[] = U"1234";
}

関数内でその関数名をconst char[]で取埗できるようになった。

const char* func(){
	return __func__;//__func__でその関数名ず同じ文字列になる
}

int main() {
	const char* str = func();
}

2進数で敎数リテラルを曞けるようになった。たた数倀リテラルで桁を区切れるようになった。

int main() {
	int x = 0b0000'1111;
	int y = 0b0011'0011;
	int z = 0b0101'0101;
}

関数にdeprecatedを指定できるようになった。MSVCではそのような関数が呌び出されようずするずコンパむル゚ラヌになる。

[[deprecated]]void depre(){}

int main() {
	depre();
}

template template parameterでtypenameず曞けるようになった。

template<template<class>typename T>struct T{};

template<class>struct U{};

int main() {
	T<U> x{};
}

std::integral_constantにoperator()が远加された。

#include<type_traits>

int main() {
	std::integral_constant<int, 4> x{};
	int y = x();
}

chronoにナヌザヌ定矩リテラルが远加された。

#include<chrono>

int main() {
	using namespace std::chrono_literals;
	constexpr auto hyaku = 100ms;
	constexpr auto nihyaku = 200s;
	constexpr auto t = hyaku + nihyaku;
}

文字列をstd::stringにするナヌザヌ定矩リテラルが远加された。

#include<string>

int main() {
	using namespace std;
	auto str = "12345"s;
}

""で囲む文字列を扱うためのstd::quotedが远加された。

#include<iostream>
#include<iomanip>
#include<string>
#include<sstream>

int main() {
	using std::operator""s;
	auto str = "test"s;
	std::cout << std::quoted(str) << std::endl;
	
	auto str2 = R"("Hello World")"s;
	std::stringstream ss{};
	ss << str2;
	ss >> std::quoted(str2);
	std::cout << str2 << std::endl;
}

フリヌ関数ずしおstd::sizeずstd::emptyずstd::dataが远加された。

#include<vector>

int main() {
	std::vector<int> vec = { 0,1,2 };
	int s = std::size(vec);
	bool e = std::empty(vec);
	auto d = std::data(vec);
}

参考
C++11の文法ず機胜(C++11: Syntax and Feature)゚ンコヌド方匏http://ezoeryou.github.io/cpp-book/C++11-Syntax-and-Feature.xhtml#encoding-scheme
巻末: C++14の新機胜 1.1 二進数リテラル(binary literal)http://ezoeryou.github.io/cpp14-appendix/#binary-literal
巻末: C++14の新機胜 1.2 桁区切り(digit separator)http://ezoeryou.github.io/cpp14-appendix/#digit-separators
巻末: C++14の新機胜 1.10 [[deprecated]]http://ezoeryou.github.io/cpp14-appendix/#deprecated_attribute
本の虫 2014-05-pre-Rapperswil-mailingのレビュヌ: N4040-N4051http://cpplover.blogspot.jp/2014/08/2014-05-pre-rapperswil-mailing-n4040.html
Faith and Brave - C++で遊がう C++14 quotedマニピュレヌタhttp://faithandbrave.hateblo.jp/entry/20131108/1383886265

終わりに
元々ブログの広告を消すために始たったこの䌁画だったが途䞭からGistに移った。 普段から最新のC++を远う局からしたら知っおるようなこずばかり曞いたが、 VS2015では䜕が䜿えお䜕が䜿えないのかを自分の䞭で敎理するこずができた。 ここたで読んで䞋さった方々には感謝の意を䌝えたい。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment