語法甜點11:非成員的begin和end 在C++ 03中,標準容器都提供了begin和end成員函數,但對於普通數組,則只能使用不同的寫法。比如:
1 vector<int> v; 2 int a[100]; 3 sort(v.begin(), v.end()); 4 sort(a, a+sizeof(a)/sizeof(a[0]));
1 sort(begin(v), end(v)); 2 sort(begin(a), end(a));
1 class Base 2 { 3 virtual void some_func(float); 4 }; 5 6 class Derived : public Base 7 { 8 virtual void some_func(int) override; // 將產生編譯錯誤 9 virtual void some_func(float) override; // 正確 10 };
1 class Base1 final { }; 2 class Derived1 : public Base1 { }; // 將產生編譯錯誤 3 class Base2 4 { 5 virtual void f() final; 6 }; 7 class Derived2 : public Base2 8 { 9 void f(); // 將產生編譯錯誤 10 };
enum class IColor1 { Red, Blue, Gree=100, Black };
1 enum class IColor2 : unsigned int { Red, Blue, Gree=100, Black }; 2 enum IColor3 : unsigned int { Red, Blue, Gree=100, Black };
1 enum class IColor1; 2 enum class IColor2 : unsigned int; 3 enum IColor3 : unsigned int;
1 template< typename first, typename second, int third> 2 class SomeType; template< typename second> 3 typedef SomeType<OtherType, second, 5> TypedefName; // 在C++ 03中是不合法的
1 template< typename first, typename second, int third> 2 class SomeType; 3 template< typename second> 4 using TypedefName = SomeType<OtherType, second, 5>;
1 typedef void (*Func)(int); 2 using Func = void (*)(int);
1 struct point 2 { 3 point() {} 4 point(int x, int y): m_x(x), m_y(y) {} 5 int m_x, m_y; 6 }; 7 union 8 { 9 int z; 10 double w; 11 point p; // 在C++ 03中不合法;在C++ 11中合法 12 };
1 u8"I'm a UTF-8 string." 2 u"This is a UTF-16 string." 3 U"This is a UTF-32 string."
1 R"(The String Data \ Stuff " )" 2 R"delimiter(The String Data \ Stuff " )delimiter"
1 u8R"XXX(I'm a "raw UTF-8" string.)XXX" 2 uR"*@(This is a "raw UTF-16" string.)*@" 3 UR"(This is a "raw UTF-32" string.)"
1 struct SomeType { OtherType member; }; 2 sizeof(SomeType::member); // 在C++ 03中不合法;在C++ 11中合法
1 int a[5] = {-2, -1, 0, 1, 2}; 2 auto funIsPositive = [](int v){return v>0;}; 3 bool bRet = all_of(a, a+5, funIsPositive); // false 4 bRet = any_of(a, a+5, funIsPositive); // true 5 bRet = none_of(a, a+5, funIsPositive); // false 6 int b[5] = {0}; 7 copy_n(a, 5, b); // 將a開始的5個元素拷貝到b中 8 copy_if(a, a+5, b, funIsPositive); // 將1, 2兩個數拷貝到b中 9 iota(a, a+5, 10); // a中的每個元素加10
1 int GetFive() { return 5; } 2 int some_value[GetFive() + 5]; // 不合法
1 constexpr int GetFive() { return 5; } 2 int some_value[GetFive() + 5];
ref
得到一個包裝引用 (至於常引用,則可以通過 cref
得到)。考慮下面的代碼:1 void f (int &r) { r++; } 2 template<class F, class P> void g (F f, P t) { f(t); } 3 4 int n = 0 ; 5 g(f, n) ; 6 cout << n << endl; // 輸出0 7 g(f, ref(n)); 8 cout << n << endl; // 輸出1
語法甜點11:非成員的begin和end
在C++ 03中,標準容器都提供了begin和end成員函數,但對於普通數組,則只能使用不同的寫法。比如:
1 vector<int> v; 2 int a[100]; 3 sort(v.begin(), v.end()); 4 sort(a, a+sizeof(a)/sizeof(a[0]));
1 sort(begin(v), end(v)); 2 sort(begin(a), end(a));
1 class Base 2 { 3 virtual void some_func(float); 4 }; 5 6 class Derived : public Base 7 { 8 virtual void some_func(int) override; // 將產生編譯錯誤 9 virtual void some_func(float) override; // 正確 10 };
1 class Base1 final { }; 2 class Derived1 : public Base1 { }; // 將產生編譯錯誤 3 class Base2 4 { 5 virtual void f() final; 6 }; 7 class Derived2 : public Base2 8 { 9 void f(); // 將產生編譯錯誤 10 };
enum class IColor1 { Red, Blue, Gree=100, Black };
1 enum class IColor2 : unsigned int { Red, Blue, Gree=100, Black }; 2 enum IColor3 : unsigned int { Red, Blue, Gree=100, Black };
1 enum class IColor1; 2 enum class IColor2 : unsigned int; 3 enum IColor3 : unsigned int;
1 template< typename first, typename second, int third> 2 class SomeType; template< typename second> 3 typedef SomeType<OtherType, second, 5> TypedefName; // 在C++ 03中是不合法的
1 template< typename first, typename second, int third> 2 class SomeType; 3 template< typename second> 4 using TypedefName = SomeType<OtherType, second, 5>;
1 typedef void (*Func)(int); 2 using Func = void (*)(int);
1 struct point 2 { 3 point() {} 4 point(int x, int y): m_x(x), m_y(y) {} 5 int m_x, m_y; 6 }; 7 union 8 { 9 int z; 10 double w; 11 point p; // 在C++ 03中不合法;在C++ 11中合法 12 };
1 u8"I'm a UTF-8 string." 2 u"This is a UTF-16 string." 3 U"This is a UTF-32 string."
1 R"(The String Data \ Stuff " )" 2 R"delimiter(The String Data \ Stuff " )delimiter"
1 u8R"XXX(I'm a "raw UTF-8" string.)XXX" 2 uR"*@(This is a "raw UTF-16" string.)*@" 3 UR"(This is a "raw UTF-32" string.)"
1 struct SomeType { OtherType member; }; 2 sizeof(SomeType::member); // 在C++ 03中不合法;在C++ 11中合法
1 int a[5] = {-2, -1, 0, 1, 2}; 2 auto funIsPositive = [](int v){return v>0;}; 3 bool bRet = all_of(a, a+5, funIsPositive); // false 4 bRet = any_of(a, a+5, funIsPositive); // true 5 bRet = none_of(a, a+5, funIsPositive); // false 6 int b[5] = {0}; 7 copy_n(a, 5, b); // 將a開始的5個元素拷貝到b中 8 copy_if(a, a+5, b, funIsPositive); // 將1, 2兩個數拷貝到b中 9 iota(a, a+5, 10); // a中的每個元素加10
1 int GetFive() { return 5; } 2 int some_value[GetFive() + 5]; // 不合法
1 constexpr int GetFive() { return 5; } 2 int some_value[GetFive() + 5];
ref
得到一個包裝引用 (至於常引用,則可以通過 cref
得到)。考慮下面的代碼:1 void f (int &r) { r++; } 2 template<class F, class P> void g (F f, P t) { f(t); } 3 4 int n = 0 ; 5 g(f, n) ; 6 cout << n << endl; // 輸出0 7 g(f, ref(n)); 8 cout << n << endl; // 輸出1