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
1
vote
2 answers

correct way to store a pointer to member function inside std::any

Looking into this question about map of member function I'm observing an anomaly in the way to pass a pointer to member function into a std::any. I'm using the following snippet: #include #include #include class VarCall { …
Oersted
  • 769
  • 16
1
vote
1 answer

C++ cast std::any to base class without knowing derived class type

A bit of an obscure issue here but I need a way to cast std::any to its base class, without knowing what derived class it is. In other words, given a base class: struct HCallable { int numArgs; std::vector argTypes; …
Gamaray
  • 51
  • 8
1
vote
3 answers

Cant cout item in container with std::any

This script #include #include #include using namespace std; int main() { unordered_map test; test[5] = "Hey!"; cout << test[5]; return 0; } Why does it not work? candidate function not…
Bfyuvf
  • 139
  • 9
1
vote
0 answers

Shared_ptr of casted object becomes empty after casting

I'm learning to use std::any and std::any_cast, it's not working as I expected: after casting, the internal shared_ptr of casted object becomes empty. Here is the simplified code to show the issue, it's hard for me to reason about why after…
Jinsong Li
  • 6,347
  • 1
  • 23
  • 20
1
vote
2 answers

C++ std::any function that convert std::any of C char-array to string

#include #include #include #include #include using namespace std; string AnyPrint(const std::any &value) { cout << size_t(&value) << ", " << value.type().name() << " "; if (auto x =…
Huy Le
  • 1,439
  • 4
  • 19
1
vote
2 answers

Rust: How to Match against Any

I would like to store any type in a Vec, and match against the actual type that is stored in the Vec. Here is my attempt: use std::any::Any; fn main() { let mut a = Vec::>::new(); a.push(Box::new(42)); …
Blue7
  • 1,750
  • 4
  • 31
  • 55
1
vote
1 answer

How to gtest / gmock function accepting std::experimental::any argument?

Issue I need a help in fixing my unit test issue with gtest 1.10.0 version. When I tried to unit test involving a function that accepts std::experimental::any argument, exception is thrown and unit test terminated. Steps to reproduce the…
1
vote
0 answers

How to iterate an `std::array` with `std::any` value type?

How to iterate an std::array with std::any value type. Suppose we don't use the std::tuple and std::get but only a std::array. I have declared it: std::array array_any = { "Hello", 50, …
Desmond Gold
  • 1,517
  • 1
  • 7
  • 19
1
vote
1 answer

Does std::any_cast call destructor? How the cast works?

#include #include using namespace std; class c { public: c() :a{ 0 } { cout << "constructor\n"; } c(int aa) :a{ aa } { cout << "Constructor\n"; } ~c() { cout << "destructor\n"; } int get() { return a; } private: …
sunkue
  • 278
  • 1
  • 9
1
vote
1 answer

Like std::function but with more varied argument and return types

I'm in search of a way to set up and call functions with arbitrary arguments and return types. One use case would be high level scripting. Something like this: // universal function using dynfunction = std::any (*)(std::vector args); I…
markus
  • 81
  • 6
1
vote
2 answers

mutable object referred by std::any

Is only the way to modify, not replace, an object stored as std::any is to declare changeable data mutable? E.g. to avoid creation and copy of class S instances: #include #include #include #include struct S…
Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
1
vote
1 answer

std::any containing std::tuple fails to compile

I realize nested std::any's are a bad idea. Nevertheless, I encountered something that's making me scratch my head and I'm just trying to understand why the compiler is choking. Consider the following line of code (assuming that the arg variable is…
Crankycyclops
  • 552
  • 5
  • 25
1
vote
3 answers

Is it possible to forward function calls with matching template type for a std::any-like container?

I haven't found a way to achieve what I want but I'm not knowledgeable enough to know if its impossible. Help would be appreciated. The main data data container in our software behaves a bit like a std::variant or std::any: It has a base class…
emmenlau
  • 958
  • 12
  • 20
1
vote
3 answers

Is it possible to create std::any with std::reference_wrapper from just std::any?

Let's say, I have an std::any that store type T in it. Is it possible to create another std::any that will contain type std::reference_wrapper? Like std::any original = std::string("Test string"); std::any reference; // Magic here auto…
val - disappointed in SE
  • 1,475
  • 3
  • 16
  • 40
1
vote
1 answer

std::any - why does it lack so many operators?

I am looking at using std::any for type erasure as part of an interface I am building, and I am left wondering why it is lacking so many operators, like "+", "==". I tinkered on Coliru here, where I extended std::any to be summable and printable…
d-karl
  • 98
  • 5