C++
Материал из Xgu.ru
C++ (си плюс плюс) — популярный язык программирования общего назначения, разработанный на основе языка Си, первоначально отличающийся от него поддержкой классов. Датой рождения считается 1983. С тех пор язык претерпел ряд существенных изменений, которые отразились как в различных реализациях языка, так и в нескольках стандартах.
[править] Главные новшества в С++11 по сравнению с С++
- автовыведение типов и ключевое слово auto;
- константа nullptr;
- лямбда-функции;
- универсальный инициализатор;
- циклы по диапазону (range-based for);
- упакованные скобки ">>" для шаблонов;
- поддержка потоков;
- поддержка атомарных операций;
- поддержка регулярных выражений;
- новые умные указатели unique_ptr, shared_ptr, weak_ptr;
- move constructor, move-оператор присвоения.
Кроме того есть существенные усовершенствования в стандартной библиотеке.
https://en.wikipedia.org/wiki/C++11
https://en.wikipedia.org/wiki/C++14
http://en.cppreference.com/w/cpp
==== Type inference (auto & decltype) ====
auto some_strange_callable_type = std::bind(&some_function, _2, _1, some_object);
auto other_variable = 5;
int some_int;
decltype(some_int) other_integer_variable = 5;
auto DeduceReturnType(); // Return type to be determined.
=== Generic lambdas ===
auto lambda = [](auto x, auto y) {return x + y;};
==== Range-based for loop ====
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;
}
// similar but also using type inference for array elements
for (auto &x : my_array) {
x *= 2;
}
// or
std::vector<int> myvec;
for (auto& iterator : myvec) {
...
}
=== Object construction improvement ===
class SomeType {
int number;
public:
SomeType(int new_number) : number(new_number) {}
SomeType() : SomeType(42) {}
};
// or
class BaseClass {
public:
BaseClass(int value);
};
class DerivedClass : public BaseClass {
public:
using BaseClass::BaseClass;
};
=== Explicit overrides and final ===
//override
struct Base {
virtual void some_func(float);
};
struct Derived : Base {
virtual void some_func(int) override; // ill-formed - doesn't override a base class method
};
//final
struct Base2 {
virtual void f() final;
};
struct Derived2 : Base2 {
void f(); // ill-formed because the virtual function Base2::f has been marked final
};
=== Null pointer constant ===
void foo(char *);
void foo(int);
in C++03: foo(NULL); // => will call foo(int)
in C++11: foo(nullptr);// => calls foo(nullptr_t), not foo(int);
=== Static assertions ===
static_assert((GREEKPI > 3.14) && (GREEKPI < 3.15), "GREEKPI is inaccurate!");
=== Template aliases ===
typedef void (*FunctionType)(double); // Old style
using FunctionType = void (*)(double); // New introduced syntax
=== Explicitly defaulted and deleted special member functions ===
struct NonCopyable {
NonCopyable() = default;
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
};
=== Threading facilities && Multithreading memory model ===
>>> http://de.cppreference.com/w/cpp/thread <<<
std::thread, std::thread::join()
For synchronization between threads, appropriate mutexes (std::mutex, std::recursive_mutex) and condition variables (std::condition_variable and std::condition_variable_any) are added to the library. These are accessible via Resource Acquisition Is Initialization (RAII) locks (std::lock_guard and std::unique_lock) and
+locking algorithms for easy use.
The new std::async facility provides a convenient method of running tasks and tying them to a std::future.
=== Thread-local storage ===
>>> http://de.cppreference.com/w/cpp/language/storage_duration <<<
Any object which could have static storage duration (i.e., lifetime spanning the entire execution of the program) may be given thread-local duration instead.
=== Regular expressions ===
>>> http://de.cppreference.com/w/cpp/regex <<<
And many mores ....
=== Hash tables ===
=== C++11 supports three Unicode encodings: UTF-8, UTF-16, and UTF-32 ===
=== General-purpose smart pointers ===
=== Extensible random number facility ===
=== Filesystem library ===
=== Atomic operations library ===
=== Localization library ===
[править] Критика С++
Несмотря на то что C++ очень популярн, у языка есть множество критиков.
В их числе:
Материалы по теме:
- The Problem With C++ (англ.) — критика от самого Бьярна Страуструпа
- C++ criticism by other people (англ.) — множество примеров слабости C++