Upgrade to Pro — share decks privately, control downloads, hide ads and more …

C++11 —— 奇怪這真的是 C++ 嗎

C++11 —— 奇怪這真的是 C++ 嗎

工讀分享

David Kuo (Davy)

April 22, 2014
Tweet

More Decks by David Kuo (Davy)

Other Decks in Education

Transcript

  1. • ઌલඃ᜝࡞ C++0xɼଈ ISO/IEC 14882:2011ɼੋ໨લత C++ ఔࣜޠݴతਖ਼ࣜඪ४ɻ༻Ҏऔ୅ C++98 Ҏٴ C++03ɻ

    • ৽తඪ४แؚ֩৺ޠݴత৽ޭೳɼฒ׌Ꮇల C++ ඪ४ఔࣜ ݿɼซೖྃେ෦෼త C++ Technical Report 1 ఔࣜݿ (Ꮠላ తಛघവࣜআ֎)ɻ • ࠜᎦ Bjarne Stroustrup (C++ త૑࢝ਓ) දࣔɼ৽తඪ४ሡ زݷ 100% ૬༰ԙݱ༗ඪ४ɻ w w C++11
  2. 1. template <typename T> 2. class MyClass { 3. public:

    4. void print(T data) { 5. std::cout << data << std::endl; 6. } 7. }; ໛൘ Template
  3. 1. template <typename T> 2. class MyClass { 3. public:

    4. void print(T data) { 5. std::cout << data << std::endl; 6. } 7. }; ໛൘ Template
  4. 1. template <typename T> 2. class MyClass { 3. public:

    4. void print(T data) { 5. std::cout << data << std::endl; 6. } 7. }; ໛൘ Template
  5. 1. template <typename T> 2. class MyClass { 3. public:

    4. void print(T data) { 5. std::cout << data << std::endl; 6. } 7. }; ໛൘ Template
  6. • MyClass.h : template class MyClass<T> ۩ݱԽ • A.cpp :

    template class MyClass<int> ۩ݱԽ • B.cpp : template class MyClass<int> ۩ݱԽ http://goo.gl/ASpqXx http://zevoid.blogspot.tw/2012/04/c11-extern-template.html
  7. • MyClass.h : template class MyClass<T> ۩ݱԽ • A.cpp :

    template class MyClass<int> ۩ݱԽ • B.cpp : template class MyClass<int> ۩ݱԽ http://goo.gl/ASpqXx http://zevoid.blogspot.tw/2012/04/c11-extern-template.html
  8. • MyClass.h : template class MyClass<T> ۩ݱԽ • A.cpp :

    template class MyClass<int> ۩ݱԽ • B.cpp : template class MyClass<int> ۩ݱԽ http://goo.gl/ASpqXx http://zevoid.blogspot.tw/2012/04/c11-extern-template.html ॏෳ
  9. • C++
 int[] numbers = {1, 2, 3, 4}; •

    C++
 std::vector<int> numbers = {1, 2, 3, 4}; ॳ࢝Խ۲ྻ Initializer lists
  10. • C++
 int[] numbers = {1, 2, 3, 4}; •

    C++
 std::vector<int> numbers = {1, 2, 3, 4}; ॳ࢝Խ۲ྻ Initializer lists
  11. • C++11
 SequenceClass someVar = {1, 4, 5, 6};
 std::vector<int>

    numbers = {1, 2, 3, 4};
 std::vector<std::string> v = { "this", "is", "awesome" }; ॳ࢝Խ۲ྻ Initializer lists
  12. • ୈҰڃత C++11 ඪ४ఔࣜݿܕผɻ • ୞ೳៃ༝ฤᩄثಁա {…} ޠ๏ඃᯩଶ஍ݐߏ ɻ •

    ҰៃݐߏศՄෳ੡ (copy-by-reference)ɻ • ሱԙৗᏐɼҰ୴ඃݐཱɼଖ੒һۉෆೳඃվᏓɼ੒һ தతࢿྉ໵ෆೳඃᏓಈɻ std::initializer_list<T>
  13. 1. struct BasicStruct 2. { 3. int x; 4. float

    y; 5. }; 6. BasicStruct var1{5, 3.2f}; ౷Ұతॳ࢝Խ Uniform initialization
  14. 1. struct AltStruct 2. { 3. AltStruct(int _x, float _y)

    : x(_x), y(_y) {} 4. private: 5. int x; 6. float y; 7. }; 8. AltStruct var2{2, 4.3f}; ౷Ұతॳ࢝Խ Uniform initialization
  15. 1. struct IdString 2. { 3. std::string name; 4. int

    identifier; 5. }; 6. IdString var3{"SomeName", 4}; ౷Ұతॳ࢝Խ Uniform initialization
  16. 1. struct IdString 2. { 3. std::string name; 4. int

    identifier; 5. }; 6. IdString var3{"SomeName", 4}; ౷Ұతॳ࢝Խ Uniform initialization
  17. 1. struct IdString 2. { 3. std::string name; 4. int

    identifier; 5. }; 6. IdString var3{"SomeName", 4}; ౷Ұతॳ࢝Խ Uniform initialization const char *
  18. 1. IdString GetString() 2. { 3. return {"SomeName", 4}; 4.

    } ౷Ұతॳ࢝Խ Uniform initialization
  19. 1. IdString GetString() 2. { 3. return {"SomeName", 4}; 4.

    } ౷Ұతॳ࢝Խ Uniform initialization ။ࣗಈॳ࢝Խ IdString
  20. • C++
 for (
 std::vector<int>::const_iterator itr = v.cbegin();
 itr !=

    v.cend();
 ++itr
 ) {…} ܕผਪಋ Type inference
  21. • C++11
 for (
 auto itr = v.cbegin();
 itr !=

    v.cend();
 ++itr
 ) {…} ܕผਪಋ Type inference
  22. • C++11
 for (
 auto itr = v.cbegin();
 itr !=

    v.cend();
 ++itr
 ) {…} ܕผਪಋ Type inference Magic !
  23. • C++
 int array[5] = {1, 2, 3, 4, 5};


    for (int i = 0; i < sizeof(my_array); i++) {…} Range-based for loop
  24. • C++11
 int array[5] = {1, 2, 3, 4, 5};


    for (int &x : array) {…} Range-based for loop
  25. • C++11
 int array[5] = {1, 2, 3, 4, 5};


    for (auto &x : array) {…} Range-based for loop
  26. • C++11
 [](int x, int y) { return x +

    y; }
 [](int x, int y) -> int { int z = x + y; return z + x; } Lambda
  27. • C++11
 [](int x, int y) { return x +

    y; }
 [](int x, int y) -> int { int z = x + y; return z + x; } Lambda auto declared
  28. • C++11
 [](int x, int y) { return x +

    y; }
 [](int x, int y) -> int { int z = x + y; return z + x; } Lambda
  29. • [] // ᔒ༗Ҿ༻೚Կ֎෦ᏓᏐɻ࢖༻ະఆٛᏓᏐ။ಋகࡨޡɻ • [x, &y] // x Ҏၚ值ํࣜၚೖ(༬ઃ)ɼy

    Ҏၚჩߟํࣜၚೖɻ • [&] // ೚Կඃ࢖༻౸త֎෦ᏓᏐօᯅࣜ஍ҎჩߟํࣜՃҎҾ༻ɻ • [=] // ೚Կඃ࢖༻౸త֎෦ᏓᏐօᯅࣜ஍Ҏၚ值ํࣜՃҎҾ༻ɻ • [&, x] // x Ҏၚ值ํࣜՃҎҾ༻ɻଖᰨᏓᏐҎჩߟํࣜՃҎҾ༻ɻ • [=, &z] // z ҎჩߟํࣜՃҎҾ༻ɻଖᰨᏓᏐҎၚ值ํࣜՃҎҾ༻ɻ Lambda
  30. 1. std::vector<int> someList; 2. int total = 0; 3. std::for_each(someList.begin(),

    someList.end(), [&total](int x) { 4. total += x; 5. }); 6. std::cout << total; Lambda
  31. 1. std::vector<int> someList; 2. int total = 0; 3. std::for_each(someList.begin(),

    someList.end(), [&total](int x) { 4. total += x; 5. }); 6. std::cout << total; Lambda
  32. 1. class SomeType { 2. int number; 3. string name;

    4. SomeType( int i, string& s ) : number(i), name(s) {} 5. public: 6. SomeType( ) : SomeType( 0, "invalid" ) {} 7. SomeType( int i ) : SomeType( i, "guest" ) {} 8. SomeType( string& s ) : SomeType( 1, s ){ PostInit(); } 9. }; ෺݅ݐߏతվྑ Object construction improvement
  33. 1. class BaseClass { 2. public: 3. BaseClass(int iValue); 4.

    }; 5. class DerivedClass : public BaseClass { 6. public: 7. using BaseClass::BaseClass; 8. }; ෺݅ݐߏతվྑ Object construction improvement
  34. 1. class MyClass { 2. int number = 12; 3.

    public: 4. MyClass() {}; 5. }; ෺݅ݐߏతվྑ Object construction improvement
  35. 1. class MyClass { 2. int number = 12; 3.

    public: 4. MyClass() {}; 5. }; ෺݅ݐߏతվྑ Object construction improvement Not in C++
  36. 1. class MyClass { 2. int number; 3. public: 4.

    MyClass() : number(12) {}; 5. }; ෺݅ݐߏతվྑ Object construction improvement C++
  37. 1. class MyClass { 2. int number = 12; 3.

    public: 4. MyClass() {}; 5. }; ෺݅ݐߏతվྑ Object construction improvement C++11
  38. 1. class BaseClass { 2. public: 3. BaseClass(int iValue); 4.

    }; 5. class DerivedClass : public BaseClass { 6. public: 7. using BaseClass::BaseClass; 8. }; ෺݅ݐߏతվྑ Object construction improvement
  39. 1. struct Base { 2. virtual void some_func(); 3. };

    4. struct Derived : Base { 5. void some_func(); 6. }; ᰖࣜ㐘വࣜॏࡌ Explicit overrides and final .
  40. 1. struct Base { 2. virtual void some_func(); 3. };

    4. struct Derived : Base { 5. void some_func(); 6. }; ᰖࣜ㐘വࣜॏࡌ Explicit overrides and final . ॏࡌ (Override)ʁ ଟܕ (Polymorphism)ʁ
  41. 1. struct Base { 2. virtual void some_func(float); 3. };

    4. struct Derived : Base { 5. virtual void some_func(int) override; 6. virtual void some_func(float) override; 7. }; ᰖࣜ㐘വࣜॏࡌ Explicit overrides and final .
  42. 1. struct Base { 2. virtual void some_func(float); 3. };

    4. struct Derived : Base { 5. virtual void some_func(int) override; 6. virtual void some_func(float) override; 7. }; ᰖࣜ㐘വࣜॏࡌ Explicit overrides and final .
  43. 1. struct Base { 2. virtual void some_func(float); 3. };

    4. struct Derived : Base { 5. virtual void some_func(int) override; 6. virtual void some_func(float) override; 7. }; ᰖࣜ㐘വࣜॏࡌ Explicit overrides and final .
  44. 1. struct Base1 final { }; 2. struct Derived1 :

    Base1 { }; 3. struct Base2 { 4. virtual void f() final; 5. }; 6. struct Derived2 : Base2 { 7. void f(); 8. }; ᰖࣜ㐘വࣜॏࡌ Explicit overrides and final .
  45. 1. struct Base1 final { }; 2. struct Derived1 :

    Base1 { }; 3. struct Base2 { 4. virtual void f() final; 5. }; 6. struct Derived2 : Base2 { 7. void f(); 8. }; ᰖࣜ㐘വࣜॏࡌ Explicit overrides and final .
  46. • C
 NULL
 0
 ((void*)0) • C++
 NULL
 0 •

    C++11
 nullptr (nullptr_t ܕผɼෆՄඃ᫚׵੒੔Ꮠɼ໵ෆೳൺֱ)
 0 ۭࢦඪ Null pointer constant
  47. • C++ ۭന
 ෆੋܕผ҆શత (not type-safe)
 all enums are integer

    (could be compared)㠷
 ๫࿐ԙҰൠൣᅴத ڧܕผྻᎯ Strongly typed enumerations.
  48. 1. enum Side{ Right, Left }; 2. enum Thing{ Wrong,

    Right }; ڧܕผྻᎯ Strongly typed enumerations.
  49. 1. enum Side{ Right, Left }; 2. enum Thing{ Wrong,

    Right }; ڧܕผྻᎯ Strongly typed enumerations. Not in C++
  50. 1. enum Side{ Right, Left }; 2. enum Thing{ Wrong,

    Right }; ڧܕผྻᎯ Strongly typed enumerations. Not in C++11
  51. 1. enum class Enumeration /* : int */ 2. {

    3. Val1, 4. Val2, 5. Val3 = 100, 6. Val4 /* = 101 */, 7. }; ڧܕผྻᎯ Strongly typed enumerations.
  52. • C++11
 લஔᡎ໌ (forward declaration) 
 ྻᎯྨ (classification of enumeration)


    ܕผ҆શ (type-safe)
 ޠኮൣᅴ (scoping) ఆٛԙྻᎯܕผత໊᜝ൣᅴத ڧܕผྻᎯ Strongly typed enumerations.
  53. • C++11
 લஔᡎ໌ (forward declaration) 
 ྻᎯྨ (classification of enumeration)


    ܕผ҆શ (type-safe)
 ޠኮൣᅴ (scoping) ఆٛԙྻᎯܕผత໊᜝ൣᅴத ڧܕผྻᎯ Strongly typed enumerations. Enumeration::Val4 == 101
  54. • C++11
 લஔᡎ໌ (forward declaration) 
 ྻᎯྨ (classification of enumeration)


    ܕผ҆શ (type-safe)
 ޠኮൣᅴ (scoping) ఆٛԙྻᎯܕผత໊᜝ൣᅴத ڧܕผྻᎯ Strongly typed enumerations. Enumeration::Val4 == 101 enum class Enum2 : unsigned int {Val1, Val2}; ҝྫ Enum2::Val1 ੋ༗ҙٛతදࣔ๏ɼ ࣕᄸᘐత Val1 ଇ൱ɻ
  55. 1. enum Enum1; 2. enum Enum2 : unsigned int; 3.

    enum class Enum3; 4. enum class Enum4: unsigned int; 5. enum Enum2 : unsigned short; ڧܕผྻᎯ Strongly typed enumerations. લஔᡎ໌ (forward declaration)
  56. 1. enum Enum1; 2. enum Enum2 : unsigned int; 3.

    enum class Enum3; 4. enum class Enum4: unsigned int; 5. enum Enum2 : unsigned short; ڧܕผྻᎯ Strongly typed enumerations. લஔᡎ໌ (forward declaration) ແ๏൑ผେখ
  57. 1. enum Enum1; 2. enum Enum2 : unsigned int; 3.

    enum class Enum3; 4. enum class Enum4: unsigned int; 5. enum Enum2 : unsigned short; ڧܕผྻᎯ Strongly typed enumerations. લஔᡎ໌ (forward declaration) ແ๏൑ผେখ Enum2 ቮඃᡎ໌ҝ unsigned int
  58. • C++
 Ұ཯ሡ ">>" ࢹҝӈҠӡࢉࢠ
 std::vector<std::pair<int, int>>
 std::vector<std::pair<int, int> >

    • C++11
 ༏ઌղੳӈׅ֯ᥒҝᒬ൘ҾᏐత݁ଋූᥒ
 std::vector<std::pair<int, int>>
 std::vector<std::pair<int, int> > ׅ֯ᥒ >>
  59. 1. template< typename A, typename B, int num> 2. class

    SomeType; 3. 4. template< typename B> 5. typedef SomeType<OtherType, B, 5> Name; ໛൘తผ໊ Alias templates Not in C++
  60. 1. template< typename A, typename B, int num> 2. class

    SomeType; 3. 4. template< typename B> 5. using Name = SomeType<OtherType, B, 5>; ໛൘తผ໊ Alias templates C++11
  61. • C++
 typedef void (*PFD)(double); • C++11
 typedef void (*PFD)(double);


    using PFD = void (*)(double); ໛൘తผ໊ Alias templates
  62. 1. template<> 2. struct count<> { 3. static const int

    value = 0; 4. }; 5. template<typename T, typename... Args> 6. struct count<T, Args...> { 7. static const int value = 1 + count<Args...>::value; 8. }; ෆఆ௕ჩᏐ໛൘ Variadic templates
  63. • C++
 "TEXT" (const char *)
 L"TEXT" (const wchar_t *)

    • C++11
 u8"I'm a UTF-8 string." (const char *)
 u"This is a UTF-16 string." (const char16_t *)
 U"This is a UTF-32 string." (const char32_t *)
 u8"This is a Unicode Character: \u2018."
 u"This is a bigger Unicode Character: \u2018."
 U"This is a Unicode Character: \u2018." ৽తࣈ۲ࣈ໘值 New string literals
  64. • C++11
 R"(The String Data \ Stuff " )"
 R"delimiter(The

    String Data \ Stuff " )delimiter" ৽తࣈ۲ࣈ໘值 New string literals