stdany

Get the size of std::any

允我心安 提交于 2020-05-27 10:47:20
问题 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) , then the size is sizeof(T) . Unfortunately, this solution only works when the types are known beforehand. Do you know any solution? 回答1: You cannot get the size of the object held by std::any (other than the workaround you've mentioned). std::any is

When should I use std::any

旧巷老猫 提交于 2020-01-29 06:34:06
问题 Since C++17 std::any is introduced. One can now write code like this #include <iostream> #include <any> #include <string> int main () { const double d = 1.2; std::any var = d; const std::string str = "Hello World"; var = str; } A double is assigned to the variable var and than a std::string was assigned to it. Why has std::any been introduced? I think this is violating the least astonishment rule , because I find it hard to think of a situation, where this can be used to express more clearly,

Cannot move std::any

旧街凉风 提交于 2020-01-16 18:02:06
问题 The following code using vptr = std::vector<std::unique_ptr<int>>; auto m = std::unordered_map<int, std::any>{}; m.try_emplace(0, move(vptr{})); Fails to compile, complaining about using of deleted copy constructor of unique_ptr . After replacing std::any with vptr in template argument this code compiles, so the issue is clearly with any How can I force std::any to be moved instead of copied? 回答1: The problem is not moving std::any, it's that std::any itself does not support move-only types

Cannot move std::any

坚强是说给别人听的谎言 提交于 2020-01-16 18:01:09
问题 The following code using vptr = std::vector<std::unique_ptr<int>>; auto m = std::unordered_map<int, std::any>{}; m.try_emplace(0, move(vptr{})); Fails to compile, complaining about using of deleted copy constructor of unique_ptr . After replacing std::any with vptr in template argument this code compiles, so the issue is clearly with any How can I force std::any to be moved instead of copied? 回答1: The problem is not moving std::any, it's that std::any itself does not support move-only types

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

强颜欢笑 提交于 2019-12-22 04:43:41
问题 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 constructed is holding only an int. —end example ] Such small-object optimization shall only be applied to types T for which is_nothrow_move_constructible_v is true. As far as I know, std::any can be easily implemented through type erasure/virtual

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

送分小仙女□ 提交于 2019-12-18 18:47:17
问题 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? 回答1: Not every library from boost makes it into the standard (and even those that do may have components removed). Generally the commitee is pretty conservative when it comes to adding to the standardlibrary (since it's next to impossible to get something removed at a later point if the

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

别等时光非礼了梦想. 提交于 2019-12-10 09:59:47
问题 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 this exact same function through a plugin (dlopen), it correctly sees the type on the any, but it cannot std::any_cast the contents. Any help would be greatly appreciated in figuring out what is causing this error. Here is my environment, MWE, and

`std::any_cast` returns a copy

瘦欲@ 提交于 2019-12-07 04:58:58
问题 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 with a non pointer type argument. I can see that the pointer version of the cast might signal intentions a bit more and might be a bit more clear but why not have the value returned be a reference like this? template<typename ValueType> ValueType& any

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

纵然是瞬间 提交于 2019-12-06 03:42:35
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 this exact same function through a plugin (dlopen), it correctly sees the type on the any, but it cannot std::any_cast the contents. Any help would be greatly appreciated in figuring out what is causing this error. Here is my environment, MWE, and resulting error. >> g++ --version g++ (GCC) 7.1.1 20170526 (Red Hat 7.1.1-2) Copyright (C) 2017 Free

`std::any_cast` returns a copy

早过忘川 提交于 2019-12-05 09:36:14
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 with a non pointer type argument. I can see that the pointer version of the cast might signal intentions a bit more and might be a bit more clear but why not have the value returned be a reference like this? template<typename ValueType> ValueType& any_cast(any* operand); instead of template <typename ValueType> ValueType* any_cast(any* operand); Further