Questions tagged [stdany]

The class any is a type-safe container for single values of any type.

Class any is included into C++ 17 standard, see here. It is a container for single values of any type.

Functions any_cast provides access to contained object.

72 questions
51
votes
8 answers

C++ std::map holding ANY type of value

Basically I want MyClass that holds a Hashmap that maps Field name(string) to ANY type of Value.. For this purpose I wrote a separate MyField class that holds the type & value information.. This is what I have so far: template class…
user3794186
  • 639
  • 2
  • 8
  • 10
33
votes
4 answers

When should I use std::any

Since C++17 std::any is introduced. One can now write code like this #include #include #include int main () { const double d = 1.2; std::any var = d; const std::string str = "Hello World"; var = str; } A…
schorsch312
  • 5,553
  • 5
  • 28
  • 57
24
votes
2 answers

Type erasing type erasure, `any` questions?

So, suppose I want to type erase using type erasure. I can create pseudo-methods for variants that enable a natural: pseudo_method print = [](auto&& self, auto&& os){ os << self; }; std::variant var = // create a variant of type A B or…
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
22
votes
4 answers

Does C++11 standard provide something like boost::any?

for example boost::function is moved almost entirely to std::function, the same is with boost::shared_ptr But I can't find std::any? Was it renamed or was not it placed in new standard at all by any reason?
dev_null
  • 1,907
  • 1
  • 16
  • 25
15
votes
1 answer

std::any for objects that can't be copy constructed

I have an object which holds a unique_ptr and as such can't be copy constructed without doing a deep copy (which I don't want). I'd like to have std::any hold that object, but the only alternatives I've found is to either make std::any hold a…
George
  • 3,521
  • 4
  • 30
  • 75
13
votes
5 answers

find an element in std::vector of std::any

I want to check whether an element exists in the vector or not. I know the below piece of code will check it. #include if ( std::find(vector.begin(), vector.end(), item) != vector.end() ) std::cout << "found"; else std::cout <<…
Arun
  • 2,247
  • 3
  • 28
  • 51
11
votes
5 answers

What is the difference between `auto` and `std::any`?

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…
Ayan Bhunia
  • 479
  • 3
  • 14
10
votes
3 answers

Is there any way to cast a std::any containing a derived pointer to a base pointer, without knowing the derived type?

Let's say I have a std::any object which may or may not contain a pointer to some derived class of a given base class B. Is there any way I can do something that: Returns a B *, if the std::any object holds something convertible to B *, or Throws…
Daniel McLaury
  • 4,047
  • 1
  • 15
  • 37
10
votes
1 answer

Is it possible to store a reference in a std::any?

I was trying some things and came to the following question: Is there a possibility to store references to a value in a std::any? I tried the following approaches: #include #include #include auto…
jan.sende
  • 750
  • 6
  • 23
10
votes
1 answer

`std::any_cast` returns a copy

I was reading the documentation for std::any_cast and I find it strange that the API has the cast either return a value to the held object or a pointer to it. Why not return a reference? A copy needs to be made every time the function is called…
Curious
  • 20,870
  • 8
  • 61
  • 146
9
votes
1 answer

Move only type adapting std::any with dummy copy constructor is safe?

I'd like to initialize std::any with a move only type variable. I found Cannot move std::any. Compile error case Before I use shared_ptr workaround from the linked answer, I tested the following code: #include #include #include…
Takatoshi Kondo
  • 3,111
  • 17
  • 36
7
votes
4 answers

Using C++17 'any' with Xcode 8.1

I am using C++ in Xcode version 8.1. I need to use the functionality of boost::any but am strongly opposed to pulling any part of Boost into our project (let's not debate it please). I see that std::any is "merged into C++17" here. I want to use…
Matthew James Briggs
  • 2,085
  • 2
  • 28
  • 58
7
votes
3 answers

How can I design storage that conforms to the standard's implementation of std::any?

The standard working draft (n4582, 20.6.3, p.552) states the following suggestion for implementations of std::any: Implementations should avoid the use of dynamically allocated memory for a small contained object. [ Example: where the object…
user2296177
  • 2,807
  • 1
  • 15
  • 26
6
votes
1 answer

C++: check if the value stored in std::any is integral

There's a value stored in std::any and I want to know if it's an integral value (char, short, int, long, both signed and unsigned) or a floating point value (float, double), or something else. If I only had to check for int values, I would do…
wouldnotliketo
  • 153
  • 1
  • 10
5
votes
0 answers

Why does std::any implementation use typeid?

It seems that std::any works just fine in GCC and Clang even when compiling with -fno-rtti. While looking at the libc++ source I see that they just use a simple trick: they take the address of a variable templated on a type in any so that is how…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
2 3 4 5