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
0
votes
1 answer

Is it possible to store and retrieve a container (e.g. std::vector) in an std::any variable?

I would like to have heterogenous map in C++ for my unit test values. Other threads recommended the use of std::any with any_cast for this purpose. This works well for primitive types like int and double but I fail to retrieve the value if I use a…
Dennix
  • 115
  • 1
  • 10
0
votes
1 answer

Is there a way to access the internal pointer of std::any using std::any_cast?

I can't get std::any running properly in the following peace of code. What I'd like to achieve is to return a reference/ pointer to the object hold by std::any. The version below using a void pointer is just running fine. How can I translate that…
andir
  • 59
  • 4
0
votes
1 answer

Why does std::function::operator= in C++ always construct and not assign object?

This query is mainly based on std::function::operator= and std::any::operator=. The documentation shows that they are always implemented by constructing a new temporary object and swapping it with this object using the swap function,…
0
votes
2 answers

Creating a vector of the type of std::any

Consider the following example #include #include #include #include #include typedef enum TYPE{ INT8=0, INT16=1, INT32=2 } TYPE; int main() { std::map myMap; …
Atharva Dubey
  • 832
  • 1
  • 8
  • 25
0
votes
1 answer

Does C++ standard guarantee such kinds of indirect access well defined?

Below has 3 different styles for indirect accessing. I am trying to understand if they all have well-defined behavior, and can be safely used for cross-platform code. #include #include #include using namespace std; #if…
0
votes
0 answers

C++ std::any how to check if type of std::any is vector

#include #include #include #include #include #include #include using namespace std; using MapAny = std::map; int square(int x) { return x*x; } vector
Huy Le
  • 1,439
  • 4
  • 19
0
votes
2 answers

C++ polymorphism factory How to build function object from its string name and map of parameter

#include #include #include #include #include #include #include #include using namespace std; using MapAny = map; class FunctionBase { public: …
Huy Le
  • 1,439
  • 4
  • 19
0
votes
1 answer

binary comparison not defined when using map

I was looking for a way to have a string[] as a map value, and I found this Stack Overflow question. I attempted to use the std::any type to solve my problem, and I got the error binary '<': 'const _Ty' does not define this operator or a conversion…
greatusername
  • 129
  • 1
  • 10
0
votes
1 answer

How to Use std::any as mapped_type

I am trying to solve the problem asked yesterday on SO based on this answer. I have modified the code given here to use std::any instead of void*. The code that i currently have is as follows: #include #include #include…
Jason
  • 36,170
  • 5
  • 26
  • 60
0
votes
1 answer

CRTP used with std::any vs virtual functions

I am trying to create a compile time polymorphism design that will not require virtual functions with all their drawbacks. However I am struggling with creating simple, effective and easy to understand container that can simulate the ability to hold…
0
votes
1 answer

How to get the data type of a std::any variable?

std::any var = 1; How can you get the type of var? For example: std::cout << GetTypeOf(var) << std::endl; Output: int
anom
  • 41
  • 6
0
votes
0 answers

Generically accessing data from a varying list of attributes within sub-classes while maintaining polymorphic behavior

With the use of constant expressions, parameter packs, lambdas, move semantics, standard containers, and std::any I can easily enough create the following design pattern: #include #include #include constexpr auto…
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
0
votes
0 answers

Why does the non-pointer std::any_cast not return a reference?

The any_cast() function - say, for a non-const reference operand - has the following signature: template T any_cast(any& operand); Why does this function not return a T& - where the documentation says the returned value is…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
0
votes
0 answers

How to find data type in C++17 any vector?

I am trying to find the data type of an element of a vector of any type. I tried the following code: #include #include #include #include int main() { std::vector myList; …
Govinda Totla
  • 576
  • 6
  • 19
0
votes
1 answer

Cannot use library in c++

Description I'm new to c++. My project needs to use any library, which is a new feature after c++ 17. After updating my compiler following this link. I still encounter an error. Here is the error message and my test code. g++ version: g++ (Ubuntu…
Harold
  • 33
  • 5