我有一个Map std::map<std::string, boost::any>,来自boost::program_options包.现在我想打印该地图的内容:
for(po::variables_map::const_iterator it = vm.begin(); it != vm.end(); ++it) {
std::cerr << it->first << ": " << it->second << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这是不可能的,因为boost::any没有operator<<定义.
打印该地图最简单的方法是什么?
我可以为任何自动尝试将每个输出运算符定义为anyint,然后是double,然后是字符串等,每次忽略错误并尝试强制转换直到转换成功并且我可以打印为指定类型.
但是在Boost中应该有一个更简单的方法吗?我需要像反向的东西lexical_cast......
对于多态性,通常的方法是使用std::vector<base*>.但是,我必须自己提供地址,即自己管理内存,无论是使用std::unique_ptr<>还是原始指针.
我想要一个polymorphic_storage<base>接受任何继承类型的类型base.我还希望将类型存储在连续的内存中,以便更快地遍历和缓存相关的问题.
但是,存在一个非常大的问题:在存储级别缺少类型信息时,必须在调整大小时调用正确的移动/复制操作.
功能要求:
我可以用什么机制来实现这个目标?
虽然我提供了答案,但我欢迎任何人发布他们的解决方案.
自从std::any引入C++ 17以来.现在可以编写这样的代码了
#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;
}
Run Code Online (Sandbox Code Playgroud)
双被分配给变量var和比std::string被分配给它.
为什么要std::any介绍?
我认为这违反了least astonishment rule,因为我发现很难想到一种情况,这可以用来表达更清楚,我想表达的内容.
有人能给我一个很好的例子吗std::any?
有一个值存储在std::any,我想知道它是整数值(char, short, int, long,有符号和无符号)还是浮点值(float, double),或其他东西。
如果我只需要检查int值,我会这样做:
std::any a;
if (a.type() == typeid(int)) {
// do some logic for int
}
Run Code Online (Sandbox Code Playgroud)
但是if (a.type() == typeid(int) || a.type() == typeid(signed char)...)在 C++ 中为所有整数类型做一个巨人似乎......很糟糕。
如果我可以以某种方式访问该类型,我可以使用is_arithmetic<T>from但我没有,我只有which 返回。type_traitsTstd::any#type()std::type_info
有没有办法在if条件中没有很多很多分离的情况下实现这一点?
(如果重要的话,我使用 C++20)
在c ++ 17中,我们有std :: any,它在内存中存储变量类型的对象.好的部分是我可以创建一个std :: any的向量来模拟任意类型对象的容器.
每当从容器一个查询返回的对象将使用 的std :: any_cast打电话时使用完全相同的类型的std :: make_any创建任何对象.以下是我如何实现这一目标的片段
#include <any>
#include <iostream>
#include <unordered_map>
#include <vector>
#include <set>
int main()
{
/* create some objects */
std::set<int> mySet = { 1, 2, 3 };
std::vector<int> myVec = { 3, 4, 5 };
std::unordered_map<int, std::vector<int>> myHash = { std::make_pair(1, myVec) };
/* create any store */
std::vector<std::any> anyStore;
anyStore.push_back(std::make_any<decltype(mySet)>(mySet));
anyStore.push_back(std::make_any<decltype(myVec)>(myVec));
anyStore.push_back(std::make_any<decltype(myHash)>(myHash));
/* get object back */
auto newSet = …Run Code Online (Sandbox Code Playgroud) 我想创建一个包含不同类型但共享相同概念的对象的向量(或数组)。
类似于Vec<Box<dyn trait>>Rust。
struct Dog {
void talk() {
std::cout << "guau guau" << std::endl;
}
};
struct Cat {
void talk() {
std::cout << "miau miau" << std::endl;
}
};
template <typename T>
concept Talk = requires(T a) {
{ a.talk() } -> std::convertible_to<void>;
};
int main() {
auto x = Dog{};
auto y = Cat{};
??? pets = {x, y};
for(auto& pet: pets) {
pet.talk();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 如果我有std::any一个std::string或一个int,我怎么能投清楚交代包含的类型呢?
std::any有type它,但我不能使用这种类型来施放.
例:
#include <any>
#include <iostream>
#include <string>
int main(void) {
std::any test = "things";
std::any test2 = 123;
// These don't work
std::string the_value = (std::string) test;
int the_value2 = (int) test2;
std::cout << the_value << std::endl;
std::cout << the_value2 << std::endl;
}
Run Code Online (Sandbox Code Playgroud) c++ ×7
c++17 ×3
c++20 ×2
stdany ×2
boost ×1
boost-any ×1
c++-concepts ×1
c++14 ×1
polymorphism ×1
printing ×1