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

Upcasting through std::any

In C++ you can pass instances of Derived to functions accepting Base, if Base is a base class of Derived. This is very useful for satisfying APIs and a very common design pattern for APIs. Currently I am faced with a situation, where I want to…
joergbrech
  • 2,056
  • 1
  • 5
  • 17
4
votes
2 answers

Get the size of std::any

Is there any way to get the size (in bytes) of the data stored by std::any? The only workaround I came up with is querying the type of its value by std::any::type and comparing the result to a list of known types like my_any.type() == typeid(T),…
plasmacel
  • 8,183
  • 7
  • 53
  • 101
4
votes
1 answer

Why is there no unsafe_any_cast for std::any?

My local version of the Boost headers (1.56.0) has the following functions defined in boost/any.hpp, copied verbatim: // Note: The "unsafe" versions of any_cast are not part of the // public interface and may be removed at any time. They are //…
Maarten Bamelis
  • 2,243
  • 19
  • 32
4
votes
1 answer

Why does an std::any_cast of a passed std::any inside a dlopen'd function raise an error

I am toying around with c++17 and plugins, and I have run into an error that I cannot get around. In the following MWE I can call a local function that takes a std::any, and everything works as expected when I try to read the contents. When I load…
3
votes
1 answer

Adequate hash from std::any

Is there an adequate way in C++ to extract a hash from the data that std::any stores? Well, or at least an object in the form of a list of bytes and its length
3
votes
1 answer

Checking std::any's type without RTTI

I'm using std::any with RTTI and exceptions disabled. It works and std::any_cast() is able to detect whether the type is correct as explained in std::any without RTTI, how does it work?. std::any::type() is disabled without RTTI though. I'd like…
Helloer
  • 417
  • 3
  • 13
3
votes
1 answer

Is this a bug in libstdc++ with std::any or std::is_copy_constructible, or is it a legit error?

I've run into a rather troubling situation in which I'm trying to use std::make_any with a class I've written which accepts std::any in one of its constructors, but in attempting to come up with a minimal test case, have come across what I think is…
vdods
  • 128
  • 4
3
votes
2 answers

any_cast with std::any's and std::optional

If I put a T into an std::any, I can get it with any_cast(my_any). But does the standard (= C++17, in the ballot phase at the moment) include a function like any_cast(optional oa) which returns nullopt if oa is nullopt and…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
2
votes
1 answer

std::any_cast without needing the type of the original object

Is it possible to use std::any_cast without putting in the first template argument (the type of the object the any is covering)? I tried using any_cast but it didn't work. Also tried to store the objects types from…
Bfyuvf
  • 139
  • 9
2
votes
1 answer

How to print an element of a string vector which was inserted using a variable of type "std::any"

Here is my c++ Code's main function. int main() { vector a; any x; x="Hello"; a.insert(a.begin(),any_cast(x)); cout<
Avi H
  • 21
  • 1
2
votes
0 answers

Can a compiler elide the bodies of functions that are address-taken but never called?

Background information Consider this simplified implementation of std::any (irrelevant parts omitted) and some code that uses it: namespace std { namespace details { // Each instance of this function template has a different address …
2
votes
1 answer

How to return a different type based on a enum argument

I have two functions which require the following: Function 1: Requires the address of a variable to set the value. ( It knows about the correct type) Function 2: Is a overloaded function which requires the value of a type. I need a way to return a…
yehi
  • 21
  • 3
2
votes
1 answer

using 'auto' in std::map

I'm parsing a JSON file, the values can consist of integer, string or float. Normally I have a map defined like this: std::map myMap; The problem is I'm not clear how to use map if there can be different data types, I…
0x29a
  • 733
  • 1
  • 7
  • 23
2
votes
1 answer

C++ Type erasure with traits

I wanted to know that this is possible to make an erased type that conforms to a trait like this one : template using read_t = std::conditional_t().Read(uint16_t{})), std::integral_constant
uknys
  • 59
  • 5
1
vote
3 answers

Interaction between std::optional and has_value()

For debugging purposes, I was writing a function which iterates over a vector of optional variables of any type to check which ones were initialized, but the check for has_value() on all of them is returning true, despite no value having ever been…