Pointers in c++ after delete

Both: A* c = a; A* d = b; are undefined in C++11 and implementation defined in C++14. This is because a and b are both “invalid pointer values” (as they point to deallocated storage space), and “using an invalid pointer value” is either undefined or implementation defined, depending on the C++ version. (“Using” includes … Read more

How to implement make_unique function in C++11? [duplicate]

Version by Stephan T. Lavavej (also known by STL) who originally proposed adding this function to C++14 #include <cstddef> #include <memory> #include <type_traits> #include <utility> namespace std { template<class T> struct _Unique_if { typedef unique_ptr<T> _Single_object; }; template<class T> struct _Unique_if<T[]> { typedef unique_ptr<T[]> _Unknown_bound; }; template<class T, size_t N> struct _Unique_if<T[N]> { typedef void … Read more

Advantages of using std::make_unique over new operator [duplicate]

Advantages make_unique teaches users “never say new/delete and new[]/delete[]” without disclaimers. make_unique shares two advantages with make_shared (excluding the third advantage, increased efficiency). First, unique_ptr<LongTypeName> up(new LongTypeName(args)) must mention LongTypeName twice, while auto up = make_unique<LongTypeName>(args) mentions it once. make_unique prevents the unspecified-evaluation-order leak triggered by expressions like foo(unique_ptr<X>(new X), unique_ptr<Y>(new Y)). (Following the advice … Read more

static_assert dependent on non-type template parameter (different behavior on gcc and clang)

Both compilers are correct. From [temp.res]/8: If no valid specialization can be generated for a template, and that template is not instantiated, the template is ill-formed, no diagnostic required. There does not exist a valid specialization that can be generated from the primary template Hitchhiker, so it is ill-formed, no diagnostic required. clang chooses to … Read more

C++11 aggregate initialization for classes with non-static member initializers

In C++11 having in-class member initializers makes the struct/class not an aggregate — this was changed in C++14, however. This is something I found surprising when I first ran into it, the rationale for this restriction is that in-class initializers are pretty similar to a user defined constructor but the counter argument is that no … Read more

Strongly typed using and typedef

Here’s a minimal complete solution that will do what you want. You can add more operators etc to make the class more useful as you see fit. #include <iostream> #include <string> #include <map> // define some tags to create uniqueness struct portal_tag {}; struct cake_tag {}; // a string-like identifier that is typed on a … Read more

How does generic lambda work in C++14?

Generic lambdas were introduced in C++14. Simply, the closure type defined by the lambda expression will have a templated call operator rather than the regular, non-template call operator of C++11‘s lambdas (of course, when auto appears at least once in the parameter list). So your example: auto glambda = [] (auto a) { return a; … Read more