C++

2181 readers
1 users here now

The center for all discussion and news regarding C++.

Rules

founded 2 years ago
MODERATORS
126
127
128
129
130
131
132
133
134
135
136
137
138
139
 
 

In c++17, std::any was added to t he standard library. Boost had their own version of "any" for quite some time before that.

I've been trying to think of a case where std::any is the best solution, and I honestly can't think of one. std::any can hold a variable of any type at runtime, which seems incredibly useful until you consider that at some point, you will need to actually use the data in std::any. This is accomplished by calling std::any_cast with a template argument that corresponds to the correct type held in the std::any object.

That means that although std::any can hold a type of any object, the list of valid objects must be known at the point that the variable is any_cast out of the std::any object. While the list of types that can be assigned to the object is unlimited, the list of types that can be extracted from the object is still finite.

That being said, why not just use a std::variant that can hold all the possible types that could be any_cast out of the object? Set a type alias for the std::variant, and there is no more boilerplate code than you would have otherwise. As an added benefit, you ensure type safety.

140
141
142
143
144
 
 

Another great "back to basics", by the great O'Dwyer. Definitely a must-watch for those still struggling with C++ smart pointers.

145
 
 

The intersection of forwarding references and overload resolution has been bugging me, and I've been caught out a few times on the wrong overload, so here's an idea.

146
 
 

Best resource I've seen out there for template basics. It even briefly mentions variadic templates, concepts that are easy to understand, auto in function parameters (a.k.a. abbreviated function templates) and how to find out what type is chosen when you do class template automatic deduction (CTAD).

I feel like this is an absolute must-watch if you want to know about modern template practices.

147
 
 

C++03 does feel ancient now, but the sizeof trick to simulate what you could do today with std::void_t and decltype (or simply concepts since C++20) definitely blew me away!

148
 
 

The talk goes over compile-time parsing using Boost.MetaParse, Lexy, CTRE, CTPG and Macro Rules, and how it fits in with reflection.

149
 
 

I quite liked this talk. Especially where Vincent talks about aggregate initialization, invariants and avoiding invalid values.

150
view more: ‹ prev next ›