11

I recently came across the std::any class, introduced in C++17, based on boost::any. This class can "hold an instance of any type" and auto automatically deduces the data type of a variable.

So what is the main difference? What are the pros and cons?

cigien
  • 57,834
  • 11
  • 73
  • 112
Ayan Bhunia
  • 479
  • 3
  • 14
  • 10
    They are not comparable because they are not interchangeable. There is no pros and cons. The type of an `auto` object is resolved during compilation. The type of an object help by a `std::any` is not. – François Andrieux Oct 19 '20 at 16:22
  • [Docs for `auto`](https://en.cppreference.com/w/cpp/language/auto). [Docs for `std::any`](https://en.cppreference.com/w/cpp/utility/any). – user4581301 Oct 19 '20 at 16:24
  • Two very different things. There is no meaningful way to compare them. – super Oct 19 '20 at 16:25
  • 2
    I haven't downvoted but this question does not show any research. Even minimal research like a google search answers it –  Oct 19 '20 at 16:26
  • std::any has nothing to do with auto. It's more comparable to std::variant. See [C++ std::variant vs std::any](https://stackoverflow.com/q/56303939/995714) – phuclv Oct 19 '20 at 16:26

5 Answers5

23

std::any and auto are completely different constructs.


std::any is a container type that can hold an object of any type:

std::any a = 42;        // a holds an int, but type is std::any
a = std::string{"hi"};  // ok, a holds a string now

The type of the object held by std::any can change during the execution of the program.


auto is a keyword that designates a placeholder type. The type of a variable with auto is the type of the value used to initialize the variable:

auto a = 42;            // a is int, for the entirety of the program
a = std::string{"hi"};  // error, a has type int

This type is determined statically, i.e. at compile time, and can never change during the execution of the program.


These constructs are not interchangeable, and so they have different use cases, and you can't compare the pros and cons of one versus the other meaningfully.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • 1
    Weird how we used the same variable name and the same integer value. I guess 42 is special. – lvella Oct 19 '20 at 16:34
  • 3
    @lvella Not too weird :) `a` is the first character in the alphabet, and 42 most certainly is special :) – cigien Oct 19 '20 at 16:36
  • @lvella Also, both `auto` and `any` start with `a`, so that explains a lot :) I think that might have been a subconscious bias for me at least. – cigien Oct 21 '20 at 17:06
4

If you write:

auto a = 42;
a = "some string";

you get a compilation error, because a is variable of type int, and you can't assign a string to it. The auto keyword just means the compiler will make the decision of what type to use for you.

Now, if you write:

std::any a = 42;
a = "some string";

That will work, because the type of a is std::any, which is a complex class type that makes use of templates to behind the scenes store any type for you. Needless to say, it is a much more complex type than int, and you should only use it when absolutely necessary.

lvella
  • 12,754
  • 11
  • 54
  • 106
0

What is the main difference?

The main difference is that auto is a compile time thing and std::any is runtime thing while it can be said that they do logically the same thing: they can store a variable of any type.

ks1322
  • 33,961
  • 14
  • 109
  • 164
  • 3
    I'm not too comfortable with the wording "*logically do the same thing*". Could you expand on what you mean by that? `auto` can only hold one type. – cigien Oct 19 '20 at 16:37
  • I mean that OP probably noticed that they can write `auto a = 42` or `std::any a = 42`. This may seem that both statements do the same or almost the same thing. But it is not because the 1st is done at compile time and the 2nd at runtime. – ks1322 Oct 19 '20 at 16:45
  • 4
    Yes, so logically *not* the same thing, right? Even if it *seems* that way. – cigien Oct 19 '20 at 16:45
  • No, they logically look the same, but they can't be absolutely the same because of the differences I mentioned. – ks1322 Oct 19 '20 at 16:49
  • 3
    Logically is still not the right word. You might mean they're syntactically the same but they are semantically(which is the logic of the statement) different. – Bobby Tables Oct 19 '20 at 17:07
0

std::any is a modern and type-safe construct for those cases that you want to have a variable that its type may change at run-time. A few example use-cases are:

  1. Reading specific values from a file/Gui that can have any types
  2. Simulating the dynamic typing behavior of an scripting language
  3. A good and type-safe candidate to wrap void* variables coming from legacy libraries

On the other hand, auto is a compiler mechanism to do a type deduction at compile time and use it for the variable used after auto. In other words, the type of the variable defined using auto cannot be changed afterwards.

TonySalimi
  • 8,257
  • 4
  • 33
  • 62
0

auto is immutable, while std::any is mutable. That means auto is determined at runtime.

StLeoX
  • 11
  • 2