2. (Very) brief history of C++
1979: Bjarne Stroustrup starts “C with
Classes”
Classes, few other things
1983: Renamed to C++
virtual functions, references, new/delete, ...
1998: ISO C++98
First ISO standard
3. (Very) brief history of C++
2003: C++03
“Bug fixes”
2011: C++11
In the works for almost 10 years
Lots of new language / standard library features
Most of it supported by all big compilers by now
(VS2012, g++4.8, clang 3.3, Intel 13.0)
4. (Very) brief future of C++
2014: C++14
“Bug fixes”
Small improvements
Starting to be picked up by compilers
2017: C++17
More functionality
Largely undefined
9. auto references?
int& foo();
auto bar = foo(); // int& or int?
// reference *not* picked up
auto bar = foo(); // int
auto& baz = foo(); // int&
10. auto constness?
const auto foo();
// constness *not* picked up
auto bar = foo(); // Abc
const auto baz = foo(); // const Abc
11. auto constness + reference?
const int& foo();
auto &bar = foo(); // int& ... ?
bar = 3; // Writing to const variable ... ?
12. auto constness + reference?
const int& foo();
auto &bar = foo(); // int& ... ?
bar = 3; // Writing to const variable ... ?
// Special case, const picked up when const + reference!
auto &bar = foo(); // const int&
bar = 3; // Compiler error!
13. auto, general rule:
Private, writable
auto foo = bar();
Private, not writable:
const auto foo = bar();
Shared, not writable:
const auto &foo = bar();
Shared, writable
auto &foo = bar();
if return value requires copy: same as private
15. Range-based for loops
C++
std::map<std::string, std::vector<int>> map;
std::map<std::string, std::vector<int>>::iterator it = map.begin();
for(it; it != map.end(); ++it)
{
}
C++11
// auto& or const auto& would be more efficient!
std::map<std::string, std::vector<int>> map;
for (auto element : map)
{
}
16. Range-based for loops
int my_array[5] = {1, 2, 3, 4, 5};
// double the value of each element in my_array:
for (int &x : my_array) {
x *= 2;
}
std::vector<int> foo;
for(int element : foo)
{
}
17. nullptr
void foo(int* p) {}
void bar(std::shared_ptr<int> p) {}
int* p1 = NULL;
int* p2 = nullptr;
if(p1 == p2)
{
}
foo(nullptr);
bar(nullptr);
bool f = nullptr;
int i = nullptr; // error: A native nullptr can only be converted
to bool or, using reinterpret_cast, to an integral type
void f(int); //#1
void f(char *);//#2
//C++03
f(0); //which f is called?
//C++11
f(nullptr) //unambiguous, calls #2
18. Override and final
class B
{
public:
virtual void f(short) {std::cout << "B::f" << std::endl;}
};
class D : public B
{
public:
virtual void f(int) {std::cout << "D::f" << std::endl;}
};
19. Override and final
class B
{
public:
virtual void f(int) const {std::cout << "B::f " << std::endl;}
};
class D : public B
{
public:
virtual void f(int) {std::cout << "D::f" << std::endl;}
};
20. Override and final
class B
{
public:
virtual void f(short) {std::cout << "B::f" << std::endl;}
};
class D : public B
{
public:
virtual void f(int) override {std::cout << "D::f" << std::endl;}
};
21. Override and final
class B
{
public:
virtual void f(short) {std::cout << "B::f" << std::endl;}
};
class D : public B
{
public:
virtual void f(int) final {std::cout << "D::f" << std::endl;}
};
22. Strongly-typed enums
enum class Options {None, One, All};
Options o = Options::All;
Type safe
int i = Options::All; // Compiler error!
Scoped
Options o = All; // Compiler error
User-specified underlying type
enum Enum3 : unsigned long {Val1 = 1, Val2};
23. Smart Pointers
boost::shared_ptr -> std::shared_ptr
boost::unique_ptr -> std::unique_ptr
boost::weak_ptr -> std::weak_ptr
Pretty much the same
Few new features in std pointers
26. static_assert
Compile time checks
static_assert((GREEKPI > 3.14) && (GREEKPI < 3.15), "GREEKPI is inaccurate!");
template<class T>
struct Check {
static_assert(sizeof(int) <= sizeof(T), "T is not big enough!");
};
template<class Integral>
Integral foo(Integral x, Integral y) {
static_assert(std::is_integral<Integral>::value, "foo() parameter must be an integral type.");
}
27. Uniform Initialization Syntax
C++
std::string s("hello");
int m=int(); //default initialization
int arr[4]={0,1,2,3};
struct tm today={0};
struct S {
int x;
int a[4];
S(): x(-1) {
a[0] = 1;
a[1] = 2;
a[2] = 4;
a[3] = 8;
}
};
C++11
std::string s{"hello"};
int m{}; //default initialization
int arr[4]={0,1,2,3};
struct tm today={0};
struct S {
int x;
int a[4];
S(): x{-1}, a{1, 2, 4, 8}
{}
};