Raymii.org
Quis custodiet ipsos custodes?Home | About | All pages | Cluster Status | RSS Feed
Store multiple types in a single std::map in C++ with std::any, just like a python dict
Published: 23-09-2020 | Author: Remy van Elst | Text only version of this article
❗ This post is over four years old. It may no longer be up to date. Opinions may have changed.
Table of Contents
In C++, everything has a type. When declaring a function you specify the return
type and for each parameter you specify what type it is, just as for regular
variables. Templates aside (those still have a type), you almost always know
what type of data you're working with. There is the auto
keyword, which can
save you a lot of typing and duplicate code, but fundamentally you're still
working with types. Since C++ 17 you can use std::any
to store anything,
without knowing the type. This is awesome for some tasks, and horrific for most
use cases. In this article I'll show an example of a std::map
with std::any
,
that behaves like a python dict
, it's able to store multiple different types
in the same container.
There are times when you'd whish C++ wasn't so strict, staticly typed. Maybe you even dream of python, or worse, javascript. But then you go and watch wat and are cured of the javascript dream. Most of those times, you're taking a shortcut and probably need to think better about the use case.
Well, since C++ 17
there is the std::any
type. Basically it's a type safe
way of working with void pointers
, forcing you cast it to the correct type,
otherwise you get a runtime exception. With std::any
, you can seperate the
storing of the (unknown) data from the handling of said data. Whenever you're
handling the data you still need to know the type, but when storing, anything
is allowed.
Ever thought you would see this being valid and compiling:
std::vector<std::any> wow {"hello", 3.14, 9ll, my_object, std::string("world")};
A use case is when you are just storing data, or just passing things around, and the responsibility of doing "stuff" with that data is elsewhere. Or you might be implementing a library, handling user data from the network (json), unknown file contents.
Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:
I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!
Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.
You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!
First I'll go over some caveats of std::any
, then a python example, ending
with the actual C++ code to have a dynamic map.
Caveats and a word of caution
A few caveats you should keep in mind. First, this only works for copy-constructable
things. If you've explicitly deleted the copy constructor you cannot put it in a
std::any
.
Second, you still always have know the type when working with things inside your
map. You cannot, at runtime, any_cast
. Every type must be known at compile time.
Only the storage side now can be (sort of) type agnostic.
Do note that if you do have the slightest idea what kind of data/types you're
going to handle, std::variant
is often a better choice. Everything you can
check at compile time, you should check, less bugs in runtime later. std::variant
has automatic storage, std::any
may use the free store, which could mean performance
impact. std::variant
can also store non-copyable things,
In the olden days
you would probably use a void*
with a static_cast
or
a (cstyle cast)
to achieve the same use case. Advantages of std::any
are
that lifetime is managed (like smart pointers) and you're forced to cast it to
a correct type.
The microsoft devblog article has more background information on std::any
,
this post is a great overview of std::any
vs std::variant
and here
is another informative article.
Python?
In Python, you can for example, just mix and match types in a dict. For example, a dict with strings and ints:
exampleDict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
Accessing that data can either be done with exampleDict["brand"]
or
exampleDict.get("brand")
. Easy peasy. No type checking, as you'd expect in
python. No type safety either.
ExampleDict in C++
If I'd want the exact exampleDict
from the python example, I think I'd create
a struct
which hold the mentioned types:
struct exampleDict {
std::string brand;
std::string model;
int year = 0;
};
This is quite rigid, extending it required changing all code using it, if you
write C++ often, you know the drill. Probably I'd not even use std::string
s but
a Model
class or an enum
. We C++ guys love our classes and multiple inheretance.
The dynamic map with std::any (C++)
Here's the code. It's in an example
class but the gist should be clear enough.
class ExampleThing {
std::map<std::string, std::any> _tVars;
public:
template <typename T>
T getValue(const std::string &key, T defaultValue) const
{
auto it = _tVars.find(key);
if (it == _tVars.end())
return defaultValue;
return std::any_cast<T>(it->second);
};
template <typename T>
void setValue(const std::string &key, T value)
{
_tVars[key] = value;
};
};
With the above code, I can recreate the python dict from earlier, without specifying the types inside of the exampleclass. I do still need to specify then on the usage side, but not on the storage side.
ExampleThing ex1;
ex1.setValue("model", "mustang");
ex1.setValue("brand", "ford");
ex1.setValue("year", 1984);
This ex1
object can be passed around, and whenever I'm ready to read the data,
I can do so:
ex1.getValue("year", -1);
Here is another usage example:
struct fortyTwo {
std::string the;
std::string is;
int ft2 {0};
} life;
ExampleThing exampleThing1;
exampleThing1.setValue("hello", std::string("world"));
exampleThing1.setValue("pi", 3.14);
exampleThing1.setValue("dolphin", life);
std::cout << exampleThing1.getValue("hello", std::string()) << std::endl;
std::cout << exampleThing1.getValue("pi", 0.0) << std::endl;
std::cout << exampleThing1.getValue("dolphin", fortyTwo()).the << std::endl;
std::cout << exampleThing1.getValue("nonexistent", 8ll) << std::endl;
Output:
world
3.14
answer
8
default value or std::optional?
The defaultValue
part is a relic of the codebase I use this in. It was easier
to refactor specific getters/setters to this generic template with a default
value, since it used the sentinel
value (an extra variable that lets us know
if what we want wasn't available, like -1
) often.
I did play with std::optional
, but it seems that it does not play well
with any_cast
, or I would have to write more code and refactor the sentinel
usage everywhere.
Final thoughts
I'm going to repeat it, as said above, if you even have the slightest idea of
what you're data is going to be, use a std::variant
. If you need to set / get
just a bunch of numbers (int/long/double
) and some text (const char*, std::string
),
apply this code but use a std::variant
. This might seem easier and more flexible,
but it comes at a cost (dynamic memory allocation, since it's syntactic sugar
around void pointers, and the variant has compile time checks).
Still, even though it feels weirds, I think this kinda cool.
Tags: any , articles , c++ , cpp , development , python , type-safe , variant , void-pointers