When should I use std::any
Asked Answered
C

4

34

Since C++17 std::any is introduced. One can now write code like this

#include <iostream>
#include <any>
#include <string>

int main () {
    const double d = 1.2;
    std::any var = d;
    const std::string str = "Hello World";
    var = str;
}

A double is assigned to the variable var and than a std::string was assigned to it.

Why has std::any been introduced?

I think this is violating the least astonishment rule, because I find it hard to think of a situation, where this can be used to express more clearly, what I like to express.

Can somebody give me a good example, when std::any is beneficial.

https://gcc.godbolt.org/z/-kepOD

Carew answered 9/10, 2018 at 7:10 Comment(7)
blogs.msdn.microsoft.com/vcblog/2018/10/04/…Hubey
If you use std::any to obfuscate code, then sure it violates the least astonishment rule. If you use it as a safer void* (see the article T.C. linked to), it's anything but astonishing.Crackbrained
use std::any where in the past you would have used void*. Which is to say, ideally, almost nowhere.Saunder
open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3804.htmlYeseniayeshiva
Will it be good alongside factory pattern?Letaletch
Is there a typo? than->then I try to fix it,but get "Edits must be at least 6 characters"Octodecimo
In my experience void* is used almost exclusively when interacting with c libraries / callbacks... and those can't be converted to std::any.Williamsen
C
35

When to Use
void* as an extremely unsafe pattern with some limited use cases, std::any adds type-safety, and that’s why it has some real use cases.

Some possibilities:

  • In Libraries - when a library type has to hold or pass anything without knowing the set of available types.
  • Parsing files - if you really cannot specify what are the supported types.
  • Message passing.
  • Bindings with a scripting language.
  • Implementing an interpreter for a scripting language
  • User Interface - controls might hold anything
  • Entities in an editor
    (ref)
Correspond answered 9/10, 2018 at 7:27 Comment(4)
"if you really cannot specify what are the supported types." Means reading "anything" into some kind of storage. OK. And how to use that data? If I have no idea what is contained in std::any, I have no idea how to handle that data. And if I know how to handle the data, I know the list of potential stored types. And that is std::variant made for. I can't catch the point... can you explain a bit?Dominga
@Dominga if the parsing code is generic and might not know all types as compile time (either supported types are dynamic or you don't want to create a dependency).Scutiform
"Implementing an interpreter for a scripting language" is a very good example. Especially combined with "Bindings with a scripting language". I found it really convenient to use std::any when implementing a constrained Python interpreter.Silverts
@Klaus: The use case is when some code knows the type being stored, but that code isn’t responsible for the lifetime (including, perhaps, making copies) of that data.Indopacific
E
9

I would summarize it as classic "use when you cannot avoid it".

I can only think of non-performance-critical implementations of dynamically typed scripting languages to represent variables from the scripting world, but even that with a stretch (Boost.Spirit/example/qi/compiler_tutorial does it without, for both the parser and the runtime).

For everything else from parsers (e.g. Boost.Spirit.X3) to library APIs (e.g. ASIO) there would usually be a faster/better/more-specific alternative, as very few things are really "anything", most are more specific than that.

  • std::variant and/or std::optional for "almost any value"
  • std::packaged_task / std::function + lambdas for "callback with arguments", which would be a case of void* in C APIs.
  • etc.

Specifically, I wouldn't blindly plug it as a replacement for a void*, as it may allocate memory on the heap, which can be deadly for high performance code.

Elohim answered 9/10, 2018 at 7:33 Comment(2)
May, but doesn't have to. The specification allows for small object optimization IUC.Crackbrained
@StoryTeller - I updated the answer with a link to the question dedicated to that. There is an answer there with per-compiler specifics. Please take a look.Elohim
U
7

std::any is a vocabulary type. When you need to store, well, some bit of anything, as a value you can use it.

There are a number of "first level" uses of it:

  1. When interacting with scripting languages which themselves have such types, it is a natural fit.

  2. When you have a property tree with highly polymorphic content, and the structure of the tree is decoupled from the producer and consumer of the tree.

  3. When replacing the equivalent of a void* chunk of data being passed through an intermediate layer who really doesn't care what it is carrying.

It can also be used as a building block in other cases. For example, std::function could choose to store its value in the std::any:

template<class R, class...Args>
struct func<R(Args...)> {
  mutable std::any state;
  R(*f)(std::any& state, Args&&...) = nullptr;
  template<class T>
  void bind(T&& t) {
    state = std::forward<T>(t);
    f = [](std::any& state, Args&&...args)->R {
      return std::any_cast<T&>(state)(std::forward<Args>(args)...);
    };
  }
  R operator()(Args...args)const {
    return f(state, std::forward<Args>(args)...);
  }
};

that is a pretty small implementation of (most of) std::function. Basically I've used any to type erase copy/move/destroy.

You can using this elsewhere for similar problems (where you are type-erasing some operation and also want to type erase copy/move/destroy), or generalize it.

Urien answered 9/10, 2018 at 15:1 Comment(2)
tbh, this type shouldn't have been called std::any. It's.. std::some(thing?)Frantic
@swift you'll have to take that up with ... well, nobody, because it ain't changing. But definitely not me.Urien
A
2

It's used in Wt, to provide a non-template interface for tabular data.

There are conversions to string for builtin and Wt types, and you can register additional conversions by specialising Wt::any_traits. This allows anything to be displayed as an entry in a table, the view classes don't have to know anything about the types they are displaying.

Ancestry answered 9/10, 2018 at 8:33 Comment(2)
can you please post a link to a code example that’s using these traits?Elohim
I don't have an example to hand. You can look in the Wt source for how it handles say WDateTimeAncestry

© 2022 - 2024 — McMap. All rights reserved.