C++11 (I)
What's new in C++11?
Auto and decltype
Use auto for type deduction (C++11 use same type deduction as function templates)
auto i = 3; //i has type int
auto &r = i; //r has type int&
decltype (i) j; //j has type int (named variable)
decltype ((i)) k; //k has type int& (lvalue)
//C++03
std::vector<int> vec;
for (std::vector<int>::iterator i = vec.begin();i != vec.end(); ++i);
//C++11
for (auto i = vec.begin(); i!+ vec.end(); ++i);
int arr[10];
for (auto i = std::begin(arr); i != std::end(arr); ++i);
Auto and functions
// C++03 - What should be the return type of add?
template <typename T1, typename T2>
??? add(T1 t1, T2 t2)
{
return t1+t2;
}
//C++11
template <typename T1, typename T2>
auto add(T1 t1, T2 t2) -> decltype(t1 + t2)
{
return t1+t2;
}
Uniform initialisation
Arrays
// C++03 and C++11
int a[] = { 1, 3 };
std::vectors
// C++03
std::vector<int> v;
v.push_back(1);
v.push_back(3);
// C++11
sstd::vector<int> v{ 1, 3 };
Structs
//C++03
struct X {
int i, j;
X (int i, int j) : i(i), j(j) {}
};
X x(3,4);
//C++11
struct X {
int i, j;
};
X x{3, 4}
std::initializer_list
// C++11
class X {
public:
X(std::initializer_list<int> lst) {
for (auto i : lst)
v.push_back(i);
}
private:
std::vector<int> v;
};
X x = { 1, 4, 7 }; // equivalent
X x2{ 3, 6, 9 };
void f(std::initializer_list<float> l);
f({ 2.3, 4.0 });
// can also be used as return value from a function
struct Y { int a, b; };
Y y = { 1.2, 3 }; // error, narrowing conversion
Range based for loop
// C++11
std::vector<int> vec;
for (auto & i : vec)
i++;
for (auto i : { 1, 4, 7 }) {}
int arr[] = { 3, 9, 12, 15 };
for (auto i : arr)
std::cout << i << std::endl;
std::map<int, int> m {
{1, 2}, {3, 4}, {5, 6}
};
for (const auto & pr : m)
std::cout << pr.first << “ “ << pr.second << std::endl;
Constructing and initialisation
// C++11
struct X {
int i = 4;// yay, just like other languages!
int j{5}; // equivalent form using uniform initialisation
};
Controlling special members functions
// C++03
struct NotCopyable {
private:
// declared but
// not implemented
NotCopyable(
const NotCopyable &);
NotCopyable & operator=(
const NotCopyable &);
};
// copying NC => linker error
struct X {
Y y;
X(int i) : y(i) {}
X(): y() {} // manual
};
// C++03
struct NotCopyable {
private:
// declared but
// not implemented
NotCopyable(
const NotCopyable &);
NotCopyable & operator=(
const NotCopyable &);
};
// copying NC => linker error
struct X {
Y y;
X(int i) : y(i) {}
X(): y() {} // manual
};
// C++11
struct NotCopyable {
NotCopyable(
const NotCopyable &) = delete
NotCopyable & operator=(
const NotCopyable &) = delete;
};
// copying NC => compiler error
// “friendly” deleted function msg
struct X {
Y y;
X(int i) : y(i) {}
X() = default; // compiler’s
};
// C++11
struct NotCopyable {
NotCopyable(
const NotCopyable &) = delete
NotCopyable & operator=(
const NotCopyable &) = delete;
};
// copying NC => compiler error
// “friendly” deleted function msg
struct X {
Y y;
X(int i) : y(i) {}
X() = default; // compiler’s
};
Comentarios
Publicar un comentario