Header files
C++2.0 新特性包括语言和标准库两个方面,后者以 header files 形式呈现。
- C++ 标准库的header files不带
.h
,例如: #include - 新式 C header files 不带
.h
,例如: #include - 旧式 C header files (带有
.h
) 仍可用,例如:#include
编译器对 C++2.0 的支持
- C++11
#define __cplusplus 201103L
- C++98 & C++03
#define __cplusplus 199711L
编程实验
文件:Test.cpp
#include
using namespace std;
int main()
{
cout << __cplusplus << endl;
return 0;
}
输出:
201103
Variadic Templates (可变参数模板)
- 《Primer C++》中文版第五版 918 页。
// 递归出口
void print()
{
}
template
void print(const T &firstArg, const Types&... args)
{
cout << firstArg << endl; // 打印第一个参数
print(args...); // 对其余的参数调用 print
}
template
void size(const Types&... args)
{
cout << "args number : " << sizeof...(args) << endl; // 产生参数数量
}
...
就是一个所谓的 pack 包
- 用于 template parameters, 就是 template parameters pack(模板参数包)
- 用于 function parameter types, 就是 function parameter types pack(函数参数类型包)
- 用于 function parameters,就是 function parameter pack (函数参数包)
编程实验
文件: main.cpp
#include
#include
using namespace std;
// 递归出口
void print()
{
}
template
void print(const T &firstArg, const Types&... args)
{
cout << firstArg << endl; // 打印第一个参数
print(args...); // 对其余的参数调用 print
}
template
void size(const Types&... args)
{
cout << "args number : " << sizeof...(args) << endl; // 获取参数数目
}
int main()
{
print(7.5, "hello", bitset<16>(377), 42);
cout << endl;
size(1,2,3,4,5,6);
return 0;
}
输出:
7.5
hello
0000000101111001
42
args number : 6
Spaces in Template Expressions
vector >; // OK in each C++ version
verctor>; // OK since C++11
nullptr and std::nullptr_t
C++11 lets youuse nullptr instead 0 or NULL
to specifya pointer refers to no value
(which differs from having an undifined value). This new feature especially helps toaviod mistakes that occurred when a null pointer was interpreted as an integral value
. For example:
(C ++ 11允许您使用nullptr而不是0或NULL来指定一个指针不指向任何值(这与具有未定义的值不同)。 此新功能特别有助于避免在将空指针解释为整数值时发生的错误。)
void f(int);
void f(void*);
f(0); // calls f(int)
f(NULL); // calls f(int) if NULL is 0, ambiguous otherwise
f(nullptr); // calls f(void*)
nullptr is a new keyword.It automatically converts into each pointer
type but not to any integral type. It has type std::nullptr_t, definedin(see Section 5.8.1,page 161), so you can now even overload operations for ths case that a null pointer is passed. Note that std::nullptr_t counts as a fundamental data type
(see Section 5.4.2, page 127).
(nullptr是一个新关键字。 它会自动转换为每种指针类型,但不会转换为任何整数类型。 它的类型为std :: nullptr_t,在
【标准库中 nullptr_t 的定义】文件:stddef.h
#if defined(_MSC_EXTENSIONS) && defined(_NATIVE_NULLPTR_SUPPORTED)
namespace std { typedef decltype(nullptr) nullptr_t; }
using ::std::nullptr_t;
编程实验
文件:Test.cpp
#include
using namespace std;
void f(int i)
{
cout << "void f(int i) : " << i << endl;
}
void f(void *p)
{
cout << "void f(void *p) : " << p << endl;
}
int main()
{
f(0);
// f(NULL);
f(nullptr);
return 0;
}
输出:
void f(int i) : 0
void f(void *p) : 0
注: f(NULL) 编译输出
error: call of overloaded ‘f(NULL)’ is ambiguous
f(NULL);
^
test.cpp:5:6: note: candidate: void f(int)
void f(int i)
^
test.cpp:10:6: note: candidate: void f(void*)
void f(void *p)
Automatic Type Deduction with auto
With C++11, you can
declare a variable or an object without specifying its specific type
by using auto. For example:
(使用C ++ 11,可以通过使用auto来声明变量或对象,而无需指定其特定类型。 例如:)
auto i = 42; // i has type int
double f();
anto d = f() // d has type double
Using auto is
especially useful where the type is a pretty long and/or complicated expression
. For example:
vector v;
auto pos = v.begin(); // pos has type vector::iterator
auto I = [](int x)->bool{ // I has the type of a lambda
// ... // taking an int and return a bool
}
The latter is an object, representing a labda.
【标准库中 auto 的使用】文件:stl_iterator.h
#if __cplusplus >= 201103L
// DR 685.
inline auto
operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
const __normal_iterator<_IteratorR, _Container>& __rhs) noexcept
-> decltype(__lhs.base() - __rhs.base())
#else
inline typename __normal_iterator<_IteratorL, _Container>::difference_type
operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
const __normal_iterator<_IteratorR, _Container>& __rhs)
#endif
编程实验
文件:Test.cpp
#include
#include
#include
#include
using namespace std;
void func1()
{
list c = {"a", "b", "c"};
list::iterator ite;
ite = find(c.begin(), c.end(), "a");
if (ite != c.end())
{
cout << *ite << endl;
}
}
void func2()
{
list c = {"a", "b", "c"};
auto ite = find(c.begin(), c.end(), "a");
if (ite != c.end())
{
cout << *ite << endl;
}
}
int main()
{
func1();
func2();
return 0;
}
输出:
a
a