036 - std::any Tutorial 1, Custom Type Conversion Operator

preview_player
Показать описание
std::any

std::variant corresponds to C style union
- a type-safe discriminated union

std::any corresponds to C style void*
- An object that can hold instances of any CopyConstructible type.

I will discuss std::any using C++17 style type functions or type traits,
also C++20 standard concepts.

We will extend std::any and create our own library to handle
std::any much more conveniently and effectively in the next few sessions.

Using-declaration

Prerequisites:

000 - (SETUP) Install Microsoft Visual Studio, GNU g++, clang++, Intel TBB

001 - (SETUP) Hello World C++20 on Command Line

017 - (SETUP) She's so hot, she's fresh out of the oven! LLVM clang++ ver. 10 on Windows.

034 - (SETUP) Install/Upgrade GNU g++ 10.1 on Windows - Procedure Entry Point Could not be Located

035 - (SETUP) Install LLVM/clang-cl on Windows, NOT LLVM/clang++ over MSYS64/MingW64

Download Source Code:

Episode 36
Рекомендации по теме
Комментарии
Автор

This use of conversion operator is great. As simple as the implementation is, I find it an amazing use.
The problem I find using std::any, though, is that we have to know what type is being stored to be able to retrieve it without error. For example, if I create a faux-heterogeneous list using std::any then every element must store a value that can be used to access its specific type. The complexity seems to grow geometrically as the number of possible types increase. I initially incorporated a simple switch then I used polymorphism and derived classes but the same problem--as the number of types increase the program grows beyond what I find reasonable.
I believe that I am overthinking and over-engineering a solution while overlooking a more practical approach. This technique has opened my eyes to possibilities as I had conceded there may be no simple way emulate dynamic typing in a strongly-typed language such as C++.

____KB
Автор

Wow, so happy to see my name in video! :-)
And I'm going to catch you pace soon!

hankchang
join shbcf.ru