问题标签 [stdany]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - 是否可以将引用存储在 std::any 中?
我正在尝试一些事情并提出以下问题:是否有可能在 a 中存储对值的引用std::any
?
我尝试了以下方法:
结果是以下输出:
然而,我期待它是:
因此,将指针传递给value
工作正常。传递 astd::reference_wrapper
也可以value
正常工作,但是int&
以某种方式传递是行不通的。我在我的代码中做错了什么,或者通常不可能将引用存储在 a 中std::any
?
c++ - 使用 std::map 和 std::any 作为值类型
我希望类 Config 能够在字符串键中存储任何值。为此目的,似乎 std::map 适合。不幸的是,这没有编译。
看起来该std::is_copy_constructible<std::tuple<const std::any&>>
特征失败了。我该如何克服呢?请在https://github.com/marchevskys/VisualEngine找到源代码。
代码思路相同如下,
在 MS Visual Studio 上编译,并且使用早于 9.1.0 g++,但不会在 g++ 9.1.0 上编译。
你可以得到同样的错误:
c++ - 如何根据枚举参数返回不同的类型
我有两个需要以下功能的功能:
功能1:需要一个变量的地址来设置值。(它知道正确的类型)
函数 2:是一个需要类型值的重载函数。
我需要一种基于枚举(指定要使用的类型)返回不同类型的方法。
我尝试使用 std::get ,因为您可以使用数字来指定类型。但是,它要求 SelectedType 是一个常量表达式,而事实并非如此。
关键是使用一个变量来避免代码重复。
考虑以下代码:
c++ - 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:
https://wandbox.org/permlink/h6HOSdgOnQYg4a6K
The code above outputs compile error because of move_only
doesn't have copy constructor.
Add copy constructor for test
I added copy constructor for test.
https://wandbox.org/permlink/kxEnIslmVnJNRSn6
Then compile successfully finished as I expected. And I got interesting output.
It seems that the copy constructor isn't called. It is surprising for me.
And I come up with the following wrapper approach.
Add dummy copy constructor wrapper
https://wandbox.org/permlink/EDhq3KPWKP9fCA9v
The copy constructor of move_only is deleted. The class wapped_move_only inherits move_only and added copy constructor.
It successfully compiled and I got the following result.
It seems that I initialized std::any with move only type using the wrapper that provides dummy copy constructor. It is more efficient to use shared_ptr if the goal is just initialize std::any with move only type. It is expected behavior for me.
As long as I do move operation only for std::any
once move_only
is moved to std::any
, is this code safe?
If std::any
is copied, then assetion failed due to copy constructor of wrapped_move_only is called. I want to know move only case's safety.
I am also not sure why std::any
's target requires copy constructor but it is not called.
templatized
If it is safe, I can improve this approach using template. The template add_dummy_copy_constructor
is a kind of adaptor.
c++ - 如何将参数包扩展为向量
编译问题错误:'std::vector' 的初始化没有匹配的构造函数
我正在构建的代码库有几个不需要可变参数模板参数的对象。我想让他们接受 std::any 的向量。我的对象以 HTML 实体命名,例如 H1、H2、PARAGRAPH。
创建对象的接口。
模板参数包扩展为 createElement 函数上的向量未编译。我使用的版本是c++17
当我调用模板函数时,我将属性对象传递给它。模板参数中的一个,类似于 HTML 实体名称,但全部大写。在参数包中是属性。属性也是对象。
例如,在模板头文件 viewManager.hpp 中定义了以下内容
而在应用程序中,如 main.cpp
如您所见,语法使用返回 numericFormat 对象的用户定义文字。
到目前为止,我所拥有的完整源代码可以在C++ Source中看到。我希望任何对象都包含数据,而不是您提到的指针。
c++ - 使用 rapidjson 读取子对象向量
这是我的json:
我设法text
像这样提取属性:
代码:
输出:
现在我需要提取array_1
为std::vector<std::vector<std::any>>
.
我想,要拥有这样的数据类型,我将不得不迭代data["array_1"]
使用 rapidjson 的类型来填充向量。
问题是,即使尝试复制我在互联网上看到的内容,我仍然无法读取里面的值data["array_1"]
。
代码:
输出:
但我需要:
编辑
我在错误的迭代器上调用了GetType
..
谢谢您的帮助
c++ - 有没有办法在不知道派生类型的情况下将包含派生指针的 std::any 转换为基指针?
假设我有一个std::any
对象,它可能包含也可能不包含指向给定基类的某个派生类的指针B
。有什么办法可以做到:
B *
如果std::any
对象包含可转换为 的东西,则返回 aB *
,或- 如果没有抛出异常?
似乎每个dynamic_cast
都std::any_cast
提供了该功能的一半,但我看不出有任何将两者结合在一起的方法。
我知道我可以通过各种方式完成这项工作,包括显式枚举可转换为的每种类型B *
,但这是所有 DRY 违规行为的根源。
示例用例:
I don't want to include a bunch of boilerplate code listing every single kind of saw in existence just so I can grab a saw -- any Saw
-- to use in Factory1
.
c++ - std::any 用于仅移动模板,其中 copy-ctor 内的 static_assert 等于编译错误,但为什么呢?
我不明白为什么只有移动模板不能通过具有 a static_assert
(如下面的代码)的 copy-ctor 来扩展,以便与std::any
在std::any::any它说 ctor #4
此重载仅在 ... std::is_copy_constructible_v<std::decay_t<ValueType>> 为真时参与重载决议。
据我所见std::is_copy_constructible<MoveOnly>::value
,给出了 true 并且永远不会调用 copy-ctor 。那么编译器怎么可能仍然抱怨static_assert
内部的copy-ctor呢?
c++ - 是否可以仅从 std::any 使用 std::reference_wrapper 创建 std::any?
比方说,我有一个std::any
商店类型 T 。是否可以创建另一个std::any
包含 type std::reference_wrapper<const T>
?喜欢
c++ - 编译器可以省略那些被地址获取但从未被调用的函数体吗?
考虑这个简化的实现std::any
(省略不相关的部分)和一些使用它的代码:
如果优化编译器可以证明std::details::dummy_function<T>
在整个程序中从未以任何方式调用过,那么编译器是否允许在编译结果中不包含 any 的函数体std::details::dummy_function<T>
(以及由此未引用__PRETTY_FUNCTION__
的 s)T
(甚至可能使它们的地址任意指针大小的整数只能保证是不同的,否则甚至可能不是有效的内存地址)?
(或者甚至不能保证不同的地址是不同T
的,这很遗憾意味着我的实现一开始就不正确?)