You are on page 1of 54

: Visual C++ MVP

Contents
0.
0.1 C++
0.2 C++ ?
1. C++0x
1.1 C++ C++0x
1.2 Visual C++
1.3 Visual C++ 9 Visual C++ 10
2. auto
2.1
2.2 C# var
2.3 C++ STL( )
2.4 'auto'
2.5 auto
2.5.1
2.5.2
2.5.3 STL

5
5
6
7
7
7
8
9
9
9
10
10
11
11
12
12

2.6

14

3. static_assert

15

3.1 assert #error


3.2 assert #error
3.3 static_assert
3.4 static_assert
3.4.1
3.4.2
3.4.3

3.5
4. (RValue Reference)
4.1 C++98/03
4.2 (LValue) (RValue)

15
15
15
16
16
16
17

17
18
18
19

Contents
4.3
4.4 Move semantics
4.5 Move Move
4.6 Move semantics
4.7 move move
4.8 std::move
4.9 VC 10 STL
4.10
4.11
4.12
4.13 Perfect Forwarding
4.14
5. (lambda)
5.1 C#
5.2 C++ STL
5.3 C++
5.3.1
5.3.2
5.3.3
5.3.4
5.3.5

5.4
5.5
5.6 STL find_if
5.7 STL
5.8
5.9
5.10
6. decltype
6.1
6.2 decltype
7. nullptr
7.1 nullptr
7.2 nullptr
7.3 nullptr
7.4 nullptr
8.

20
21
22
23
24
25
26
28
29
30
30
34
35
35
36
37
37
37
38
38
39

39
43
44
45
46
46
47
48
48
48
49
49
50
50
50
52

0.

C++
CPU ,
CPU
.
C++
.

C++ ?
.
@whatthepaul
?
. -_-;
,
@birdkr
,
.
C++ .
. C++
C++ .
C++ ,
. C++ .
.

5
Visual C++ 10 C++0x

C++ ,
(STL)
C C
. .

0.

0.1 C++

0.2 C++ ?

0.


. ?

Visual C++ 10 C++0x

.
.
.

, AI .
HD CPU
.
C# Java C++
.

. C# Java . C++
.
C++ C++ ,
. C++
. C++
Visual C++

C++ , C++
C# Java
C++ .
C++ . C++
.
C++0x (C++0x C++98
C++03 ).
2009 2006
. C++0x 2009
2010 . 2009 C++0x
C++0x .
C++0x C++ (STL)
.

1.2 Visual C++


C++ C++ Bjarne Stroustrup C++
100% . C++
.
Visual C++ 6( VC6) Visual C++ 7( VC 7) VC 6
VC 7 .
VC 6 .
VC 6 VC 7 VC 6 C++98
VC 6 . VC 7
VC 6
.
VC 7 100% ( 100% C++
) VC 7 Visual C++ 10( VC 10)
.
VC 7, VC 8, VC 9 VC 10
.

7
Visual C++ 10 C++0x

1.1 C++ C++0x

1. C++0x

1. C++0x

1. C++0x

1.3 Visual C++ 9 Visual C++ 10

Visual C++ 10 C++0x

C++ C++0x
tr1 VC 9 VC 9 Visual
Studio Service Pack VC 9 .
VC 10 VC 9 tr1 .
C++
.
VC 10 C++0x VC 9 tr1
C++ .
VC 10 C++0x auto, static_assert, RValue Reference, lambda,
decltype, nullptr.
C++
. C++ C++
C++0x .

2. auto
Ruby Python . Ruby Python

. C++
.

2. auto

2.1

< 2-1. C/C++ >


<>
void BuyItem()
{
int Money = 500;
........
}

</>

< 2-2. Ruby >


<>
def BuyItem
Money = 500;
......
end

</>

2.2 C# var
C# C++ var type
. type .
C# var
. var C# LINQ .
< 2-3. LINQ var >
<>
// LINQ var
IEnumerable<IGrouping<string, Student>> studentQuery3 =
from student in students group student by student.Last;

Visual C++ 10 C++0x

9
. type
. Ruby, Python type
.

// LINQ var
var studentQuery3 = from student in students group student by student.Last;

2. auto

</>

< 2-3> var LINQ


var .

Visual C++ 10 C++0x

10
2.3 C++ STL( )
C++ STL . list STL
.
< 2-4. C++ list >
<>
list<int> NumList;
NumList.push_back( 10 );
for( list<int>::iterator iter = NumList.begin();
iter != NumList.end();
++iter )
{

</>

list (map
).
list type .
< 2-4> < 2-3> var
. C++ C# var ?
C++0x var < 2-4> .
C++0x auto
.

2.4 'auto'
auto type . auto
type . C# var type .
, auto .
auto type
C++ auto
( C++0x C++ auto
C# ).

auto C# var , .
type
.

2. auto

auto .

2.5.1
auto .
< 2-5. auto >
<>
#include <iostream>
using namespace std;
int main()
{
// char*
auto NPCName = "BugKing";
cout << NPCName << endl;
// int
auto Number = 1;
cout << Number << endl;

getchar();
return 0;
}

</>
< 2-5 >

, const .

< 2-6. , , const >


<>
#include <iostream>
using namespace std;
int main()
{
int UserMode = 4;
auto* pUserMode = &UserMode;
cout << "pUserMode : Value - " << *pUserMode << ", address : " << pUserMode <<
endl;

11
Visual C++ 10 C++0x

2.5 auto

2. auto

auto& refUserMode = UserMode;


refUserMode = 5;
cout << "UserMode : Value - " << UserMode << " | refUserMode : Value - " <<
refUserMode << endl;

getchar();
return 0;
}

</>

12
Visual C++ 10 C++0x

< 2-6 >

2.5.2
.
< 2-7. auto >
<>
struct CharacterInvenInfo
{
int SlotNum;
int ItemCode;
};
........
auto* CharInven = new CharacterInvenInfo();
........

</>

2.5.3 STL
auto STL .
auto STL
typedef auto .

<>
typedef std::list<MCommand*> LIST_COMMAND;
LIST_COMMAND::iterator iter = m_listCommand.begin();

</>

.
<>

auto iter = m_listCommand.begin();

</>

2. auto

auto STL .

< 2-8. vector auto >

13

<>

Visual C++ 10 C++0x

#include <iostream>
#include <vector>
using namespace std;
struct Item
{
int ItemCode;
int Money;
int SkillCode;
};
int main()
{
cout << "Use vector Iterator - 1" << endl;
vector< int > ItemCodeList;
ItemCodeList.push_back( 20 );
ItemCodeList.push_back( 30 );
ItemCodeList.push_back( 40 );
for( auto IterPos = ItemCodeList.begin(); IterPos != ItemCodeList.end(); ++IterPos)
{
cout << "ItemCode : " << *IterPos << endl;
}
cout << endl;

cout << "Use vector Iterator - 2" << endl;


vector< Item > ItemList;
Item item1;
item1.ItemCode = 1;
item1.Money = 100;
Item item2;
item2.ItemCode = 2;
item2.Money = 200;
Item item3;
item3.ItemCode = 3;
item3.Money = 300;

item1.SkillCode = 0;
item2.SkillCode = 10;
item3.SkillCode = 0;

ItemList.push_back( item1 );
ItemList.push_back( item2 );
ItemList.push_back( item3 );
for( auto IterPos = ItemList.begin(); IterPos != ItemList.end(); ++IterPos)
{
cout << "ItemCode : " << IterPos->ItemCode << ", Money : " << IterPos>Money << endl;
}
cout << endl;
getchar();
return 0;
}

</>

(Process Enactment)

(Visibility)

(Traceability)

1 ALM 3

2. auto

< 2-8 >

Visual C++ 10 C++0x

14
auto C++
. C++ .

2.6
auto
1. type
2. type
3. ,

C++
assert #error . ( #ifdef
).
assert #error assert assert , ,
.
assert ( ).
< 3-1. assert >
</>
PLAYER* pPlayer;

assert( NULL != pPlayer )

</>

3.2 assert #error


assert , #error (
) .
.
C++0x static_assert .
static_assert .
Stack type
static_assert .

3.3 static_assert
static_assert .

static_assert constant-expression
(
,error-message'') ;

constant-expression-
error-message
false

(Process
Enactment)
error

(Visibility)

constant-expression
false

(Traceability)

.

static_assert .
1 ALM 3

15
Visual C++ 10 C++0x

3.1 assert #error

3. static_assert

3. static_assert

3. static_assert

1. (int, long ) (class, struct )



2.

Visual C++ 10 C++0x

16

3.4 static_assert
3.4.1
< 3-2. >
<>
#include <iostream>
using namespace std;
const int MAX_LEVEL = 120;
int main()
{
static_assert( MAX_LEVEL <= 100, "Warring - MAX_LEVEL" );
return 0;
}

</>
< 3-1. < 3-2> >

< 3-2> MAX_LEVEL 100 .


VC++ 10 IntelliSense
.
< 3-2. >

3.4.2
< 3-3. Stack >
<>
#include <iostream>
using namespace std;

private:
T1* data;
};

Visual C++ 10 C++0x

17

int main()
{
MYSTACK< int, 5 > MyStack;
return 0;
}

</>
< 3-3. < 3-3> >

3.4.3
< 3-4. int >
<>
#include <iostream>
using namespace std;
int main()
{
static_assert( sizeof(int) == 4, "not int size 4" );
return 0;
}

</>

static_assert ,
.

3.5
1. assert
2.
3.
(Process Enactment)

(Visibility)

(Traceability)

1 ALM 3

3. static_assert

template< typename T1, int StackSize >


class MYSTACK
{
static_assert( StackSize >= 10, "Stack Size Error" );
public :
MYSTACK() : data( new T[StackSize] )
{
}

4. (RValue Reference)

4. (RValue Reference)
4.1 C++98/03
C++98/03
. .

Visual C++ 10 C++0x

18
int . RVO
NRVO
.



(a) vector ,
(a) string .
< 4-1. vector >

< 4-1> vector


. .
vector ,
.

.
. . C++ 03 (a) (b)
.
( (a) (b) , (?) )

4. (RValue Reference)

< 4-2. >

Visual C++ 10 C++0x

19

C++0x .
.
(RValue Reference).
C++0x
,
, .

.

4.2 (LValue) (RValue)



.

.
C++ .

,

. C .
< 4-3. C >

(Process Enactment)

(Visibility)

(Traceability)

1 ALM 3

4. (RValue Reference)
Visual C++ 10 C++0x

20

< 4-3> C C++ . C


C++ .
C++03 3.10/1 .

.
*ptr, ptr[index], ++x .
( ; ) . 1729, x+y,
std::string(
C++
) x++ .
, , .
? .
. =
< 4-4. >

4.3
C++ . &
.
< 4-5. refA a >

refA A . refA refA A


. refA A .

C++0x RValue Reference .


&
&&
.
<>

4. (RValue Reference)

&
LValue Reference
. LValue Reference
C++ 98 .

int&& RrefA = 119;

&

.
< 4-6. >

< 4-6> b) , d)
.
, .

4.4 Move semantics


4
Move semantics .
Move semantics C++0x move
,move

. move move &&


.
move move
.
.
move
,move

move
,
move
.
STL vector .
(Process Enactment)

STL vector (
(Visibility)

2) . C++0x
Move

semantics
(Traceability)


.

1 ALM 3

21
Visual C++ 10 C++0x

</>

4. (RValue Reference)

< 4-7. C++98/03 C++0x vector >

Visual C++ 10 C++0x

22

C++0x STL Move semantics . C++0x


. VC 10
STL Move semantics .

4.5 Move Move


C++0x move
move

. move move .
< 4-1. QuestInfo >
<>
class QuestInfo
{
public:
//
QuestInfo(const QuestInfo& quest)
: Name(new char[quest.NameLen]), NameLen(quest.NameLen)
{
memcpy(Name, quest.Name, quest.NameLen);
}
//
QuestInfo& operator=(const QuestInfo& quest)
{
if (this != &quest)
{
if (NameLen < quest.NameLen)
{
//
}
NameLen = quest.NameLen;
memcpy(Name, quest.Name, NameLen);
}
return *this;
}

Name = quest.Name;
NameLen = quest.NameLen;
quest.Name = NULL;
quest.NameLen = 0;
}
return *this;
}
private:
char* Name;
int NameLen;
};

</>

< 4-1> QuestInfo(QuestInfo&& quest) move , QuestInfo&


operator=(QuestInfo&& quest) move .
, &
&&

.

4.6 Move semantics


.
? < 4-1> Move , Move
. .
?
Move Semantics
. < 4-1> Move Move
.
< 4-2. Move >
<>
//
(Process
Enactment)

QuestInfo(const
QuestInfo&
quest)
: Name(new char[quest.NameLen]), NameLen(quest.NameLen)

{ (Visibility)
memcpy(Name, quest.Name, quest.NameLen);

} (Traceability)

1 ALM 3

4. (RValue Reference)

// move
QuestInfo& operator=(QuestInfo&& quest)
{
if( this != &quest )
{
delete Name;

23
Visual C++ 10 C++0x

// move
QuestInfo(QuestInfo&& quest)
: Name(quest.Name), NameLen(quest.NameLen)
{
quest.Name = NULL;
quest.NameLen = 0;
}

4. (RValue Reference)

// move
QuestInfo(QuestInfo&& quest)
: Name(quest.Name), NameLen(quest.NameLen)
{
quest.Name = NULL;
quest.NameLen = 0;
}

</>

Visual C++ 10 C++0x

24
< 4-2> move
. .

NULL .

.

4.7 move move


move move
.
< 4-3. >
<>
#include <iostream>
using namespace std;

class NPC
{
public:
int NPCCode;
string Name;
NPC() { cout << " << endl; }
NPC( int _NpcCode, string _Name ) { cout << " << endl; }
NPC(NPC& other) { cout << " << endl; }
NPC& operator=(const NPC& npc) { cout << " << endl; return *this; }
NPC(NPC&& other) { cout << "Move " << endl; }
NPC& operator=(const NPC&& npc) { cout << "Move " << endl; return *this; }
};
int main()
{
cout << "1" << endl;
NPC npc1( NPC( 10,"Orge1") );
cout << endl << "2" << endl;
NPC npc2(11,"Orge2");
NPC npc3 = npc2;
cout << endl << "3" << endl;
NPC npc4; NPC npc5;
npc5 = npc4;

getchar();
return 0;
}

</>
< 4-3 >

< 4-3> move move NPC


move move . < 4-3>
std::move . move
?

4.8 std::move
<>
cout << endl << "5" << endl;
NPC npc7; NPC npc8;
npc8 = std::move(npc7);

</>

< 4-3> npc8 npc7 move


.
move
move

(Process
Enactment)


. move .
(Visibility)

std::move .
(Traceability)

1 ALM 3

4. (RValue Reference)

cout << endl << "5" << endl;


NPC npc7; NPC npc8;
npc8 = std::move(npc7);

25
Visual C++ 10 C++0x

cout << endl << "4" << endl;


NPC npc6 = NPC(12, "Orge3");

4. (RValue Reference)
Visual C++ 10 C++0x

26

std::move Move Semantics . (LValue)


(RValue) .
std::move .
<>
namespace std {
template <class T>
inline typename remove_reference<T>::type&& move(T&& x)
{
return x;
}
}

</>

std::move
.

4.9 VC 10 STL
VC 10 STL .
VC 10 .

VC 10 STL vector
VC 10 vector vector .
<>

#if _HAS_RVALUE_REFERENCES
vector(_Myt&& _Right)
: _Mybase(_Right._Alval)
{
// construct by moving _Right
_Assign_rv(_STD forward<_Myt>(_Right));
}

</>

? move .

< 4-4. vector >


<>
vector<int> foo()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
return v;
}

4. (RValue Reference)

</>

< 4-4> VC 9(visual Studio 2008) VC 10 vector


.
VC 9 .
< 4-8. VC 9 vector >

vector vector
, vector .
< 4-9. VC 10 vector >

(Process Enactment)

(Visibility)

(Traceability)

1 ALM 3

27
Visual C++ 10 C++0x

int main()
{
vector<int> v1 = foo();
cout << v1[0] << endl;
return 0;
}

4. (RValue Reference)

VC 10 move .
.
vector
C++0x Move Semantics
.

Visual C++ 10 C++0x

28
STL string
string .
<>
string msg1(Error);
string msg2(- Network);
string msg3(: Accept);
string Msg = msg1 + + msg2 + + msg3;

</>

string operator+()

.

4.10
Move Semantics .
< 4-10. >

< 4-10> npc7 std::move .


move npc7
std::move npc7 .
STL vector .
< 4-5. Vector >
<>
int main()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(12);

cout << v1.size() << endl;


cout << v2.size() << endl;
return 0;
}

</>

4. (RValue Reference)

vector<int> v2 = std::move(v1);

29
Visual C++ 10 C++0x

< 4-5 >

< 4-5> v1 v2 move std::move() .


std::move .
<>
vector<int> v2 = std::move(v1);

</>

v1 0 .
Move Semantics
.
.

4.11
.
< 4-6. >
<>
struct ITEM {};
void GetItem( ITEM& item )
{
cout << "LValue " << endl;
}
void GetItem( ITEM&& item )
{
cout << "RValue " << endl;
}
int main()
{
(Process Enactment)
ITEM item;
GetItem( item );
(Visibility)
GetItem( ITEM() );

(Traceability)
1 ALM 3

4. (RValue Reference)

getchar();
return 0;
}

</>
< 4-6 >

Visual C++ 10 C++0x

30

4.12
. .
.
.
< 4-11. >

4.13 Perfect Forwarding


.
.

.

#include <iostream>
struct X {};
void func( X&& t )
{
std::cout << "RValue" << std::endl;
}
void func( X& t )
{
std::cout << "LValue" << std::endl;
}
template<typename T>
void foo(T&& t)
{
func( t );
}

int main()
{
X x;
foo(x);
foo( X() );
getchar();
return 0;
}

</>
< 4-7 >

< 4-7>
.
foo std::move .
< 4-8 >
<>
#include <iostream>
struct X {};

4. (RValue Reference)

<>

31
Visual C++ 10 C++0x

< 4-7. >

4. (RValue Reference)
Visual C++ 10 C++0x

32

void func( X&& t )


{
std::cout << "RValue" << std::endl;
}
void func( X& t )
{
std::cout << "LValue" << std::endl;
}
template<typename T>
void foo(T&& t)
{
func( std::move(t) );
}

int main()
{
X x;
foo(x);
foo( X() );
getchar();
return 0;
}

</>
< 4-8 >

std::move foo .
. std::move foo
.
. < 4-8> foo .
std::forward .
std::forward std::move .
std::forward , .
< 4-7> < 4-8> std::forward .
< 4-9. std::forward >
<>
#include <iostream>
struct X {};
void func( X&& t )

template<typename T>
void foo(T&& t)
{
func( std::forward<T>(t) );
}

int main()
{
X x;
foo(x);
foo( X() );
getchar();
return 0;
}

</>
< 4-9 >

std::forward .
<>
namespace std {
template <class T, class U,
class = typename enable_if<
(is_lvalue_reference<T>::value ?
is_lvalue_reference<U>::value :
true) &&
is_convertible<typename remove_reference<U>::type*,
typename remove_reference<T>::type*>::value
>::type>
inline T&&
forward(U&& u)
{
return static_cast<T&&>(u);
}
}

</>

4. (RValue Reference)

void func( X& t )


{
std::cout << "LValue" << std::endl;
}

33
Visual C++ 10 C++0x

{
std::cout << "RValue" << std::endl;
}

4. (RValue Reference)
Visual C++ 10 C++0x

34

4.14
1. ,
2.&
LValue Reference
.
&
&&

3. , .
4. Move semantics . Move
semantics C++0x move
,move

5.move
,move

move
,
move
.
6. std::move .
7. .
8. .
9. std::forward , .

.
. decltype sizeof
.

5. (lambda)

5. (lambda)

5.1 C#
C# .
C#
.

C#
.
.
=> or
.

.
< 5-1. TextLength >
<>
string[] MobNames = { "Babo", "Cat", "Orge", "Tester", "CEO" };
int TextLength = 4;
//
var ShortNames1 = MobNames.Where(MobName => MobName.Length < TextLength);
// foreach
List<string> ShortNames2 = new List<string>();
foreach(string MobName in MobNames)
{
if (MobName.Length < TextLength)
{
ShortNames2.Add(MobName);
}
}

</>

Visual C++ 10 C++0x

35
C++0x C++ .

5. (lambda)

< 5-1> List


foreach List
.
C# LINQ .
LINQ LINQ .

Visual C++ 10 C++0x

36
5.2 C++ STL
C++ STL find_if, sort
(functer) .
.
< 5-2. find_if >
<>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class User
{
public:
User() : m_bDie(false) {}
~User() {}
void SetDie() { m_bDie = true; }
bool IsDie() { return m_bDie; }
private:
bool m_bDie;
};
struct FindDieUser
{
bool operator()
};

(User& tUser) const { return tUser.IsDie(); }

int main()
{
vector< User > Users;
User tUser1;
Users.push_back(tUser1);
User tUser2;
tUser2.SetDie();
Users.push_back(tUser2);
User tUser3;
Users.push_back(tUser3);
vector< User >::iterator Iter;
Iter = find_if( Users.begin(), Users.end(), FindDieUser() );
return 0;
}

</>

< 5-2> find_if struct


. ?

5.3.1
.
<>
int main()
{
[] // lambda capture
() //
{} //
() // ;
}

</>

.
<>
int main()
{
[]{ std::cout << Hello, TechDay!<< std::endl; }();
}

<>

5.3.2
auto .
< 5-3. >
<>
#include <iostream>
int main()
{
auto func = [] { std::cout << "Hello, TechDay!" << std::endl; };
func();
getchar();
return 0;
}

</>
< 5-3 >

37
Visual C++ 10 C++0x

5.3 C++

5. (lambda)

< 5-1> C# < 5-2> find_if


? C++0x .

5.3.3

5. (lambda)

.
< 5-4. >
<>
#include <iostream>

Visual C++ 10 C++0x

38

template< typename Func >


void Test( Func func )
{
func();
}
int main()
{
auto func = [] { std::cout << "Gunz2 is Greate Game!" << std::endl; };
Test( func );
getchar();
return 0;
}

</>
< 5-4 >

5.3.4
.
< 5-5. >
<>
#include <iostream>
int main()
{
auto func = []( int n ) { std::cout << "Number : " << n << std::endl; };
func( 333 );
func( 7777 );
getchar();
return 0;
}

</>
< 5-5 >

< 5-6. >


<>
int main()
{
auto func1 = [] { return 3.14; };
auto func2 = [] ( float f ) { return f; };
auto func3 = [] () -> float{ return 3.14; };
float f1 = func1();
float f2 = func2( 3.14f );
float f3 = func3();
return 0;
}

</>

< 5-6> .
< 5-1. < 5-6> >

< 5-1> func1 3.14 double , func2 float


float . func3 float
3.14 func1 dobule float .
< 5-6> func3
->
.

5.4

(Capture).
. &
,
.

39
Visual C++ 10 C++0x

. ,
.

5. (lambda)

5.3.5


<>
5. (lambda)

[]() { }

</>

[] .

40
Visual C++ 10 C++0x


< 5-7> .
< 5-7. lambda >
<>
int main()
{
vector< int > Moneys;
Moneys.push_back( 100 );
Moneys.push_back( 4000 );
Moneys.push_back( 50 );
Moneys.push_back( 7 );
int TotalMoney1 = 0;
for_each(Moneys.begin(), Moneys.end(), [&TotalMoney1](int Money) {
TotalMoney1 += Money;
});
cout << "Total Money 1 : " << TotalMoney1 << endl;
return 0;
}

</>
< 5-7 >

TotalMoney1 Moneys
.
< 5-8> .
< 5-8. >
<>
.
int TotalMoney2 = 0;
int* pTotalMoney2 = &TotalMoney2;
for_each(Moneys.begin(), Moneys.end(), [pTotalMoney2](int Money) {
*pTotalMoney2 += Money;
});
cout << "Total Money 2 : " << TotalMoney2 << endl;
..

</>

5. (lambda)

< 5-8 >


TotalMoney1 ?
<>
for_each(Moneys.begin(), Moneys.end(), [TotalMoney1](int Money) {
TotalMoney1 += Money;
});

</>

.
error C3491: 'TotalMoney1': a by-value capture cannot be modified in a non-mutable
lambda
mutable
.

<>
[=]() mutable{ std::cout << x << std::endl; x = 200; }();

</>


< 5-7> ?
.
[] .
<>
[ &Numb1, &Numb2 ]

</>

[&]
?
. [=] .

Visual C++ 10 C++0x

41

< 5-9. >

5. (lambda)

<>
int main()
{
vector< int > Moneys;
Moneys.push_back( 100 );
Moneys.push_back( 4000 );
Moneys.push_back( 1001 );
Moneys.push_back( 7 );

42
Visual C++ 10 C++0x

int TotalMoney1 = 0;
int TotalBigMoney = 0;
// Money 1000 TotalBigMoney .
for_each(Moneys.begin(), Moneys.end(), [&](int Money) {
TotalMoney1 += Money;
if( Money > 1000 ) TotalBigMoney += Money;
});
cout << "Total Money 1 : " << TotalMoney1 << endl;
cout << "Total Big Money : " << TotalBigMoney << endl;
return 0;
}

</>
< 5-9 >

default
( ) ( ) .
default ( )
.
< 5-2. default >

,
. friend private
. this
.
< 5-10. >

5. (lambda)

5.5

class NetWork
{
public:
NetWork()
{
SendPackets.push_back(10);
SendPackets.push_back(32);
SendPackets.push_back(24);
}
void AllSend() const
{
for_each(SendPackets.begin(), SendPackets.end(), [this](int i){ Send(i); });
}
private:
vector< int > SendPackets;
void Send(int PacketIndex) const
{
cout << "Send Packet Index : " << PacketIndex << endl;
}
};
int main()
{
NetWork().AllSend();
return 0;
}

</>
< 5-10 >

< 5-10> Network AllSend() .


<>
for_each( SendPackets.begin(), SendPackets.end(),
[this](int i){ Send(i); });

</>

[]this
, Network private Send
. .

Visual C++ 10 C++0x

43

<>

5.6 STL find_if

5. (lambda)

< 5-2> .

Visual C++ 10 C++0x

44

< 5-11. find_if lambda >


<>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class User
{
public:
User() : m_bDie(false) {}
~User() {}
void SetIndex(int index) { m_Index = index; }
int
GetIndex() { return m_Index; }
void SetDie() { m_bDie = true; }
bool IsDie() { return m_bDie; }
private:
int m_Index;
bool m_bDie;
};
int main()
{
vector< User > Users;
User tUser1;
tUser1.SetIndex(1);
Users.push_back(tUser1);
User tUser2;
tUser2.SetIndex(2);
tUser2.SetDie();
Users.push_back(tUser2);
User tUser3;
tUser3.SetIndex(3);
Users.push_back(tUser3);
vector< User >::iterator Iter;
Iter = find_if( Users.begin(), Users.end(), [](User& tUser) -> bool { return
true == tUser.IsDie(); } );
cout << "found Die User Index : " << Iter->GetIndex() << endl;
return 0;
}

</>
< 5-11 >

find_if < 5-2>


<>
struct FindDieUser
{
bool operator()
};

(User& tUser) const { return tUser.IsDie(); }

Iter = find_if( Users.begin(), Users.end(), FindDieUser() );

< 5-11> .
<>
Iter = find_if( Users.begin(), Users.end(), [](User& tUser) -> bool {
return true == tUser.IsDie();
} );

</>

<>
STL
STL for while
.
.
VC 10 STL .
.
Vector Container Iterating ( http://npteam.net/775 )
</>

5.7 STL
STL function STL .
int vector .
< 5-12. vector >
<>
<iostream>
<algorithm>
<functional>
<vector>

using namespace std;

int main()
{
vector<function<int()>> v;
v.push_back( [] { return 100; } );
v.push_back( [] { return 200; } );
cout << v[0]() << endl;
cout << v[1]() << endl;
}

</>

Visual C++ 10 C++0x

45


.
STL .

#include
#include
#include
#include

5. (lambda)

</>

5. (lambda)

< 5-12 >

Visual C++ 10 C++0x

46

5.8
5.7 vector STL function
.
< 5-13. >
<>
#include <iostream>
#include <functional>
#include <string>
std::function< void() > f()
{
std::string str("lambda");
return [=] { std::cout << "Hello, " << str << std::endl; };
}
int main()
{
auto func = f();
func();
f()();
return 0;
}

</>

5.9
.
< 5-14. >
<>
int main()
{
function<int(int)> fact = [&fact](int x) {
return x == 0 ? 1 : x * fact(x - 1);
};
cout << fact(3) << endl;
}

</>

fact .

1. .
2. . decltype sizeof
3. .
4. .
5. . friend .
6. C++ .

5. (lambda)

5.10

47
Visual C++ 10 C++0x

. C++
. C++ .
Visual C++ 10 Concurrency Runtime(ConRT) .
.

6. decltype

6. decltype
6.1
(type)
.

48
Visual C++ 10 C++0x

C++0x auto decltype .


decltype auto .

6.2 decltype
.
< 6-1. decltype >
<>
int Hp;
decltype(Hp) NPCHp = 5;
decltype(Hp + NPCHp) TotalHp;
decltype(Hp*) pHp = &Hp;

</>

decltype(Hp) NPCHp = 5;
Hp int int NPCHp = 5 .
decltype(Hp + NPCHp) TotalHp;
int int int TotalHp; .
decltype(Hp*) pHp = &Hp; int* pHp = &Hp .

.
< 6-2. decltype >
<>
int foo();
decltype(foo()) value;

</>

decltype(foo()) value; int value; .

7.1 nullptr
C++03 NULL 0 .
NULL 0 int
.
< 7-1. >
<>
#include <iostream>
using namespace std;
void func( int a )
{
cout << "func - int " << endl;
}
void func( double *p )
{
cout << "func - double * " << endl;
}

int main()
{
func( static_cast<double*>(0) );
func( 0 );
func( NULL );
getchar();
return 0;
}

</>
< 7-1 >

func double* func .


func func( doube* p )
int func( int a ) .
nullptr .

49
Visual C++ 10 C++0x

nullptr C++0x (Null Pointer) .

7. nullptr

7. nullptr

7.2 nullptr
C++0x nullptr nullptr .

7. nullptr

< 7-2. nullptr >

Visual C++ 10 C++0x

50

<>
const class {
public:
template <class T>
operator T*() const
{
return 0;
}
template <class C, class T>
operator T C::*() const
{
return 0;
}
private:
void operator&() const;
} nullptr = {};

</>

7.3 nullptr
. ^^
0 NULL .
<>
char* p = nullptr;

</>

< 7-1> func( double* p )


<>
func( nullptr );

</>

7.4 nullptr

<>
char* ch = nullptr; // ch .
sizeof( nullptr ); // . 4 .

typeid( nullptr ); // .
throw nullptr; // .

<>
int n = nullptr; // int nullptr .
Int n2 = 0
if( n2 == nullptr ); //
if( nullptr ); //
if( nullptr == 0 ); //
nullptr = 0; //
nullptr + 2; //

</>

VC++ 10 0 NULL
nullptr C++
.

nullptr ?
nullptr
nullptr nullptr .
Herb Sutter Microsoft C++/CLI
nullptr .

51
Visual C++ 10 C++0x

7. nullptr

</>

8.

8.

Visual C++ 10 C++0x

52

C++0x
http://en.wikipedia.org/wiki/C%2B%2B0x
static_assert
http://en.wikipedia.org/wiki/C%2B%2B0x#Static_assertions
Rvalue Reference
http://blogs.msdn.com/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10part-2.aspx
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html
http://www.artima.com/cppsource/rvalue.html
http://cpplover.blogspot.com/2009/11/rvalue-reference_23.html
http://cpplover.blogspot.com/2009/11/rvalue-reference_25.html
lambda
http://blogs.msdn.com/vcblog/archive/2008/10/28/lambdas-auto-and-static-assert-c-0xfeatures-in-vc10-part-1.aspx
http://cpplover.blogspot.com/2009/11/lambda.html
http://cpplover.blogspot.com/2009/12/lambda.html
decltype
http://blogs.msdn.com/vcblog/archive/2009/04/22/decltype-c-0x-features-in-vc10-part-3.aspx
nullptr
http://d.hatena.ne.jp/faith_and_brave/20071002/1191322319
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf
http://ja.wikibooks.org/wiki/More_C%2B%2B_Idioms/nullptr
http://d.hatena.ne.jp/KZR/20080328/p1

: Visual C++ MVP



, Microsoft Visual C++ MVP . C++ C#
,
. (http://jacking.tistory.com/) (@jacking75) ,
.

()
892 5 () 135-777
: 1577-9700, : http://www.microsoft.com/visualstudio, : http://msdn.microsoft.com

You might also like