内容简介:I have started updating myself with Modern C++ a while ago, and since my posts, "There are many other advanced C++ concepts and idioms other than the ones I list in this article, but I would consider these 7 as "should-knows". To explain them, I have taken
I have started updating myself with Modern C++ a while ago, and since my posts, " 21 new features of Modern C++ to use in your project " and " All about lambda function in C++ " were popular, I decided to write about advanced C++ concepts and idioms that I have learned from this wikibook and course .
There are many other advanced C++ concepts and idioms other than the ones I list in this article, but I would consider these 7 as "should-knows". To explain them, I have taken a more pragmatic approach and weighed more on readability, simplicity over other fancy features, syntax sugar, and complexity.
Note: There are also drawbacks to using some of these techniques which I have not discussed here or maybe I am not qualified enough.
1. RAII
Intent: To guarantee the release of resource(s) at the end of a scope.
Implementation: Wrap a resource into a class; resource acquired in the constructor immediately after its allocation; and automatically released in the destructor; resource used via an interface of the class;
Also known as: Execute-around object, Resource release is finalization, Scope-bound resource management.
Problem
- R esource A cquisition I s I nitialization idiom is a very powerful and vastly used idiom although the name is really terrible, as the idiom is rather about resource release than acquisition.
- RAII guarantees the release of resources at the end of a scope/destruction. It thus ensures no resource leaks and provides a basic exception safety guarantee.
C++
xxxxxxxxxx
1
19
1
struct resource
2
{
3
resource(int x, int y) { cout << "resource acquired\n"; }
4
~resource() { cout << "resource destroyed\n"; }
5
};
6
7
void func()
8
{
9
resource *ptr = new resource(1, 2);
10
int x;
11
std::cout << "Enter an integer: ";
12
std::cin >> x;
13
if (x == 0)
14
throw 0; // the function returns early, and ptr won't be deleted!
15
if (x < 0)
16
return; // the function returns early, and ptr won't be deleted!
17
// do stuff with ptr here
18
delete ptr;
19
}
-
In the above code, the early
return
orthrow
statement, causes the function to terminate withoutptr
being deleted. -
Consequently, the memory allocated for the variable,
ptr
, is now leaked (and leaked again every time this function is called and returns early).
Solution
C++
xxxxxxxxxx
1
28
1
template<class T>
2
class smart_ptr
3
{
4
T* m_ptr;
5
public:
6
template<typename... Args>
7
smart_ptr(Args&&... args) : m_ptr(new T(std::forward<Args>(args)...)){}
8
~smart_ptr() { delete m_ptr; }
9
10
smart_ptr(const smart_ptr& rhs) = delete;
11
smart_ptr& operator=(const smart_ptr& rhs) = delete;
12
13
smart_ptr(smart_ptr&& rhs) : m_ptr(exchange(rhs.m_ptr, nullptr)){}
14
smart_ptr& operator=(smart_ptr&& rhs){
15
if (&rhs == this) return *this;
16
delete m_ptr;
17
m_ptr = exchange(rhs.m_ptr,nullptr);
18
return *this;
19
}
20
T& operator*() const { return *m_ptr; }
21
T* operator->() const { return m_ptr; }
22
};
23
24
void func()
25
{
26
auto ptr = smart_ptr<resource>(1, 2); // now ptr guarantee the release of resource
27
// ...
28
}
-
Note that no matter what happens after the
ptr
declaration,ptr
will be destroyed when the function terminates (regardless of how it terminates). -
As the
ptr
is a local object, the destructor will be called while the function stack frame rewinds. Hence, we are assured that theresource
will be properly cleaned up.
Use Cases
-
Using RAII, resources, like
new
/delete
,malloc
/free
, acquire/release, mutex lock/unlock, file open/close, count++
/--
, database connect/disconnect, or anything else that exists in limited supply, can easily be managed. -
Examples from C++ Standard Library include
std::unique_ptr
,std::ofstream
,std::lock_guard
, etc.
2. Return Type Resolver
Intent : To deduce the type of the object being initialized or assigned to.
Implementation: Uses a templatized conversion operator.
Also known as: Return type overloading.
Issue
C++
xxxxxxxxxx
1
1
int from_string(const char *str) { return std::stoi(str); }
2
float from_string(const char *str) { return std::stof(str); } // error
- A function cannot be overloaded only by its return type.
Solution
C++
xxxxxxxxxx
1
15
1
class from_string
2
{
3
const string m_str;
4
public:
5
from_string(const char *str) : m_str(str) {}
6
template <typename type>
7
operator type(){
8
if constexpr(is_same_v<type, float>) return stof(m_str);
9
else if (is_same_v<type, int>) return stoi(m_str);
10
}
11
};
12
13
int n_int = from_string("123");
14
float n_float = from_string("123.111");
15
// Will only work with C++17 due to `is_same_v`
If you are unaware of constexpr
, I have written a short post on when to use const vs constexpr in c++
.
Use Cases
-
When you use
nullptr
(introduced in C++11), this is the technique that runs under the hood to deduce the correct type, depending upon the pointer variable it is assigning to. - You can also overcome the function overloading limitation on the basis of a return type as we have seen above.
- Return Type Resolver can also used to provide a generic interface for assignment , independent of the object assigned to.
3. Type Erasure
Intent: To create generic container that can handle a variety of concrete types.
Implementation:
Can be implemented by void*
, templates, polymorphism, union, proxy class, etc.
Also known as: Duck-typing.
Problem
- C++ is a statically typed language with strong typing. In statically typed languages, object type is known and set at compile-time. While in dynamically typed languages, the type is associated with run-time values.
- In strongly typed languages the type of an object doesn't change after compilation.
-
To overcome this limitation & providing a feature like dynamically typed languages, library designers come up with various generic container kind of things like
std::any
(C++17),std::variant
(C++17),std::function
(C++11), etc.
Different Type Erasure Techniques
- There is no one strict rule on how to implement this idiom; it can have various forms with its own drawbacks as follows:
=> Type erasure using void* (like in C)
C++
xxxxxxxxxx
1
1
void qsort (void* base, size_t num, size_t size,
2
int (*compare)(const void*,const void*));
Drawback : not safe, and a separate compare function is needed for each type.
=> Type erasure using templates
C++
xxxxxxxxxx
1
1
template <class RandomAccessIterator>
2
void sort(RandomAccessIterator first, RandomAccessIterator last);
Drawback : may lead to many function template instantiations and longer compilation time
=> Type erasure using polymorphism
C++
xxxxxxxxxx
1
1
struct base { virtual void method() = 0; };
2
struct derived_1 : base { void method() { cout << "derived_1\n"; } };
3
struct derived_2 : base { void method() { cout << "derived_2\n"; } };
4
5
// We don't see a concrete type (it's erased) though can dynamic_cast
6
void call(base* ptr) { ptr->method(); };
Drawback: run-time cost (dynamic dispatch, indirection, vtable, etc.).
=> Type erasure using union
C++
xxxxxxxxxx
1
1
struct Data {};
2
union U {
3
Data d; // occupies 1 byte
4
std::int32_t n; // occupies 4 bytes
5
char c; // occupies 1 byte
6
~U() {} // need to know currently active type
7
}; // an instance of U in total occupies 4 bytes.
Drawback: not type-safe.
Solution
std::any
C++
xxxxxxxxxx
1
44
1
struct any
2
{
3
struct base {};
4
5
template<typename T>
6
struct inner: base{
7
inner(T t): m_t{std::move(t)} {}
8
T m_t;
9
static void type() {}
10
};
11
12
any(): m_ptr{nullptr}, typePtr{nullptr} {}
13
template<typename T>
14
any(T && t): m_ptr{std::make_unique<inner<T>>(t)}, typePtr{&inner<T>::type} {}
15
template<typename T>
16
any& operator=(T&& t){
17
m_ptr = std::make_unique<inner<T>>(t);
18
typePtr = &inner<T>::type;
19
return *this;
20
}
21
private:
22
template<typename T>
23
friend T& any_cast(const any& var);
24
25
std::unique_ptr<base> m_ptr = nullptr;
26
void (*typePtr)() = nullptr;
27
};
28
29
template<typename T>
30
T& any_cast(const any& var)
31
{
32
if(var.typePtr == any::inner<T>::type)
33
return static_cast<any::inner<T>*>(var.m_ptr.get())->m_t;
34
throw std::logic_error{"Bad cast!"};
35
}
36
37
int main()
38
{
39
any var(10);
40
std::cout << any_cast<int>(var) << std::endl;
41
var = std::string{"some text"};
42
std::cout << any_cast<std::string>(var) << std::endl;
43
return 0;
44
}
Especially, the thing here to note is how we are leveraging empty static method i.e. inner<T>::type()
to determine template instance type in any_cast<T>
.
Usecases
- Employ to handle multiple types of the return value from function/method(Although that's not recommended advice).
4. CRTP
Intent: To achieve static polymorphism.
Implementation: Make use of base class template specialization.
Also known as: Upside-down inheritance, Static polymorphism
Problem
C++
xxxxxxxxxx
1
23
1
struct obj_type_1
2
{
3
bool operator<(const value &rhs) const {return m_x < rhs.m_x;}
4
// bool operator==(const value &rhs) const;
5
// bool operator!=(const value &rhs) const;
6
// List goes on. . . . . . . . . . . . . . . . . . . .
7
private:
8
// data members to compare
9
};
10
11
struct obj_type_2
12
{
13
bool operator<(const value &rhs) const {return m_x < rhs.m_x;}
14
// bool operator==(const value &rhs) const;
15
// bool operator!=(const value &rhs) const;
16
// List goes on. . . . . . . . . . . . . . . . . . . .
17
private:
18
// data members to compare
19
};
20
21
struct obj_type_3 { ...
22
struct obj_type_4 { ...
23
// List goes on. . . . . . . . . . . . . . . . . . . .
operator < operator <
Solution
- C uriously R ecurring T emplate P attern implementation rule is simple, separate out the type-dependent & independent functionality and bind type independent functionality with the base class using template specialization .
- The above line may seem cryptic at first. So, consider the below solution to the above problem for more clarity:
C++
xxxxxxxxxx
1
31
1
template<class derived>
2
struct compare {};
3
struct value : public compare<value>
4
{
5
value(const int x): m_x(x) {}
6
bool operator<(const value &rhs) const { return m_x < rhs.m_x; }
7
private:
8
int m_x;
9
};
10
11
template <class derived>
12
bool operator>(const compare<derived> &lhs, const compare<derived> &rhs) {
13
// static_assert(std::is_base_of_v<compare<derived>, derived>); // Compile time safety measures
14
return (static_cast<const derived&>(rhs) < static_cast<const derived&>(lhs));
15
}
16
17
/* Same goes with other operators
18
== :: returns !(lhs < rhs) and !(rhs < lhs)
19
!= :: returns !(lhs == rhs)
20
>= :: returns (rhs < lhs) or (rhs == lhs)
21
<= :: returns (lhs < rhs) or (rhs == lhs)
22
*/
23
24
int main()
25
{
26
value v1{5}, v2{10};
27
cout <<boolalpha<< "v1 == v2: " << (v1 > v2) << '\n';
28
return 0;
29
}
30
// Now no need to write comparator operators for all the classes,
31
// Write only type dependent `operator <` & use CRTP
Usecases
- CRTP is widely employed for static polymorphism without bearing the cost of virtual dispatch mechanism. Consider the following code we have not used virtual keyword and still achieved the functionality of polymorphism(specifically static polymorphism).
C++
xxxxxxxxxx
1
19
1
template<typename specific_animal>
2
struct animal {
3
void who() { implementation().who(); }
4
private:
5
specific_animal& implementation() {return *static_cast<specific_animal*>(this);}
6
};
7
8
struct dog : public animal<dog> {
9
void who() { cout << "dog" << endl; }
10
};
11
12
struct cat : public animal<cat> {
13
void who() { cout << "cat" << endl; }
14
};
15
16
template<typename specific_animal>
17
void who_am_i(animal<specific_animal> & animal) {
18
animal.who();
19
}
- CRTP can also be used for optimization. As we have seen above, it also enables code reusability.
Update: The above hiccup of declaring multiple comparisons operator will permanently be sorted from C++20 by using the spaceship
( <=>
)/ Three-way-comparison operator
.
5. Virtual Constructor
Intent: To create a copy or new object without knowing its concrete type.
Implementation: Exploits overloaded methods with polymorphic assignment.
Also known as: Factory method/design-pattern.
Problem
- C++ has the support of polymorphic object destruction using it's base class's virtual destructor . Equivalent support for creation and copying of objects is missing as С++ doesn't support virtual constructor, copy constructors.
- Moreover, you can’t create an object unless you know its static type, because the compiler must know the amount of space it needs to allocate. For the same reason, copy of an object also requires its type to known at compile-time.
C++
xxxxxxxxxx
1
17
1
struct animal {
2
virtual ~animal(){ cout<<"~animal\n"; }
3
};
4
5
struct dog : animal {
6
~dog(){ cout<<"~dog\n"; }
7
};
8
9
struct cat : animal {
10
~cat(){ cout<<"~cat\n"; }
11
};
12
13
void who_am_i(animal *who) { // not sure whether dog would be passed here or cat
14
// How to `create` the object of same type i.e. pointed by who ?
15
// How to `copy` object of same type i.e. pointed by who ?
16
delete who; // you can delete object pointed by who
17
}
Solution
- The Virtual Constructor technique allows polymorphic creation and copying of objects in C++ by delegating the act of creation and copying the object to the derived class through the use of virtual methods.
-
Following code is not only implement virtual constructor(i.e.
create()
) but also implements virtual copy constructor (i.e.clone()
).
C++
xxxxxxxxxx
1
21
1
struct animal {
2
virtual ~animal() = default;
3
virtual std::unique_ptr<animal> create() = 0;
4
virtual std::unique_ptr<animal> clone() = 0;
5
};
6
7
struct dog : animal {
8
std::unique_ptr<animal> create() { return std::make_unique<dog>(); }
9
std::unique_ptr<animal> clone() { return std::make_unique<dog>(*this); }
10
};
11
12
struct cat : animal {
13
std::unique_ptr<animal> create() { return std::make_unique<cat>(); }
14
std::unique_ptr<animal> clone() { return std::make_unique<cat>(*this); }
15
};
16
17
void who_am_i(animal *who) {
18
auto new_who = who->create();// `create` the object of same type i.e. pointed by who ?
19
auto duplicate_who = who->clone(); // `copy` object of same type i.e. pointed by who ?
20
delete who; // you can delete object pointed by who
21
}
Usecases
- To provide a generic interface to produce/copy a variety of classes using only one class.
6. SFINAE and std::enable_if
Intent: To filter out functions that do not yield valid template instantiations from a set of overloaded functions.
Implementation:
Achieved automatically by the compiler or exploited using std::enable_if
.
Motivation
- S ubstitution F ailure I s N ot A n E rror is a language feature (not an idiom) a C++ compiler uses to filter out some templated function overloads during overload resolution.
- During overload resolution of function templates, when substituting the explicitly specified or deduced type for the template parameter fails, the specialization discarded from the overload set instead of causing a compile error.
- Substitution failure happens when type or expression is ill-formed.
C++
xxxxxxxxxx
1
12
1
template<class T>
2
void func(T* t){ // Single overload set
3
if constexpr(std::is_class_v<T>){ cout << "T is user-defined type\n"; }
4
else { cout << "T is primitive type\n"; }
5
}
6
7
int primitive_t = 6;
8
struct {char var = '4';} class_t;
9
10
11
func(&class_t);
12
func(&primitive_t);
- How can we create two sets(based on primitive type & user-defined type separately) of a function having the same signature?
Solution
C++
xxxxxxxxxx
1
1
template<class T, typename = std::enable_if_t<std::is_class_v<T>>>
2
void func(T* t){
3
cout << "T is user-defined type\n";
4
}
5
6
template<class T, std::enable_if_t<std::is_integral_v<T>, T> = 0>
7
void func(T* t){ // NOTE: function signature is NOT-MODIFIED
8
cout << "T is primitive type\n";
9
}
-
The above code snippet is a short example of exploiting SFINAE using
std::enable_if
, in which first template instantiation will become equivalent tovoid func<(anonymous), void>((anonymous) * t)
and second,void func(int * t)
. -
You can read more above
std::enable_if
here .
Usecases
-
Together, with
std::enable_if
, SFINAE is heavily used in template metaprogramming. - The standard library also leveraged SFINAE in most type_traits utilities. Consider the following:
C++
xxxxxxxxxx
1
17
1
// Stolen & trimmed from https://stackoverflow.com/questions/982808/c-sfinae-examples.
2
template<typename T>
3
class is_class_type {
4
template<typename C> static char test(int C::*);
5
template<typename C> static double test(...);
6
public:
7
enum { value = sizeof(is_class_type<T>::test<T>(0)) == sizeof(char) };
8
};
9
10
struct class_t{};
11
12
int main()
13
{
14
cout<<is_class_type<class_t>::value<<endl; // 1
15
cout<<is_class_type<int>::value<<endl; // 0
16
return 0;
17
}
-
Without SFINAE, you would get a compiler error, something like "
0
cannot be converted to member pointer for a non-class typeint
", as both the overload oftest
only differs in terms of the return type. -
Because
int
is not a class, it can't have a member pointer of typeint int::*
.
7. Proxy
Intent: To achieve intuitive functionality using middleware class.
Implementation: By use of temporary/proxy class.
Also known as:
operator []
(i.e. subscript) proxy, double/twice operator overloading.
Motivation
-
Most believes this is only about the subscript operator (i.e.
operator[ ]
), but I believe the type that comes in between exchanging data is the proxy. -
We have already seen a nice example of this idiom indirectly above in type-erasure
(i.e. class
any::inner<>
). But still, I think one more example will add concreteness to our understanding.
operator [ ] solution
C++
21
1
template <typename T = int>
2
struct arr2D{
3
private:
4
struct proxy_class{
5
proxy_class(T *arr) : m_arr_ptr(arr) {}
6
T &operator[](uint32_t idx) { return m_arr_ptr[idx]; }
7
private:
8
T *m_arr_ptr;
9
};
10
T m_arr[10][10];
11
public:
12
arr2D::proxy_class operator[](uint32_t idx) { return arr2D::proxy_class(m_arr[idx]); }
13
};
14
15
int main()
16
{
17
arr2D<> arr;
18
arr[0][0] = 1;
19
cout << arr[0][0];
20
return 0;
21
}
Usecases
-
To create intuitive features like double operator overloading,
std::any
etc.
Summary and FAQs
When to actually use RAII?
When you have a set of steps to carry out a task, and two steps are ideal (i.e. set-up and clean-up), then that's the place you can employ RAII.
Why can't functions be overloaded by return type?
You can't overload on return types, as it is not mandatory to use the return value of the functions in a function call expression. For example, I can just say:
get_val();
What does the compiler do now?
When to use return type resolver idiom?
You can apply return type resolver idiom when your input types are fixed but output types may vary.
What is type erasure in C++?
Type erasure technique is used to design generic types that rely on the type of assignment
(as we do in Python). By the way, do you know auto
, or can you design one now?
Best scenarios to apply type erasure idiom?
It's useful in generic programming. It can also be used to handle multiple types of the return value from function/method(Although that's not recommended advice).
What is the curiously recurring template pattern (CRTP)?
CRTP occurs when a class A
has a base class. And that base class is a template specialization for the class A
itself. E.g.
template <class T>
class X{...};
class A : public X<A> {...};
It is curiously recurring, isn't it?
Why Curiously Recurring Template Pattern (CRTP) works?
I think this answer is very appropriate.
What is SFINAE?
Substitution F ailure I s N ot A n E rror is a language feature(not an idiom) a C++ compiler uses to filter out some templated function overloads during overload resolution.
What is Proxy Class in C++?
A proxy is a class that provides a modified interface to another class.
Why do we not have a virtual constructor in C++?
A virtual-table (vtable) is made for each Class having one or more 'virtual-functions'. Whenever an object is created of such a class, it contains a 'virtual-pointer', which points to the base of the corresponding vtable. Whenever there is a virtual function call, the vtable is used to resolve to the function address.
A constructor cannot be virtual because when the constructor of a class is executed, there is no vtable in memory. This means no virtual pointer is defined yet. So, the constructor should always be non-virtual.
Can we make a class copy constructor virtual in C++?
Similar to "Why do we not have a virtual constructor in C++?" which already has been answered above.
What are the use cases and need for virtual a constructor?
To create and copy the object (without knowing its concrete type) using a base class polymorphic method.
以上所述就是小编给大家介绍的《7 Advanced C++ Concepts You Should Know》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
我看电商3:零售的变革
黄若 / 电子工业出版社 / 2018-4 / 49
在《我看电商3:零售的变革》之前,黄若先生的“我看电商”系列图书《我看电商》《再看电商》《我看电商2》,均为行业畅销书。黄若先生的图书有两大特如一是干货满满,二是观点鲜明。 “新零售”是眼下的热门词。在2017年里,数以万计的企业以“新零售”作为标识进入市场。但是社会上对“新零售“存在着各种模糊的定义和不尽相同的解读。 《我看电商3:零售的变革》中明确提出:新零售不应过分关注于渠道形式......一起来看看 《我看电商3:零售的变革》 这本书的介绍吧!