相关疑难解决方法(0)

如何将boost :: any打印到流中?

我有一个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......

c++ printing boost boost-any

16
推荐指数
3
解决办法
8713
查看次数

如何在连续内存中以相同的继承层次结构多态存储和访问不同类型?

对于多态性,通常的方法是使用std::vector<base*>.但是,我必须自己提供地址,即自己管理内存,无论是使用std::unique_ptr<>还是原始指针.

我想要一个polymorphic_storage<base>接受任何继承类型的类型base.我还希望将类型存储在连续的内存中,以便更快地遍历和缓存相关的问题.

但是,存在一个非常大的问题:在存储级别缺少类型信息时,必须在调整大小时调用正确的移动/复制操作.

功能要求:

  • 可以将任何从基类继承的类型添加到存储中; 没有固定的继承层次结构.
  • 继承类型必须在存储类型内正确对齐.
  • 必须调用正确的移动和复制操作,因为我没有处理POD类型.

我可以用什么机制来实现这个目标?


虽然我提供了答案,但我欢迎任何人发布他们的解决方案.

c++ polymorphism memory-management c++14

14
推荐指数
2
解决办法
263
查看次数

我什么时候应该使用std :: any

自从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

https://gcc.godbolt.org/z/-kepOD

c++ c++17 stdany

10
推荐指数
3
解决办法
2087
查看次数

C++:检查存储在 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++ stdany c++20

5
推荐指数
1
解决办法
123
查看次数

std :: any的商店对象类型的可能性

在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)

c++ metaprogramming c++-standard-library c++17

4
推荐指数
1
解决办法
1841
查看次数

如何创建共享概念的对象向量?

我想创建一个包含不同类型但共享相同概念的对象的向量(或数组)。
类似于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)

c++ c++-concepts c++20

4
推荐指数
1
解决办法
368
查看次数

将std :: any转换为未知类型

如果我有std::any一个std::string或一个int,我怎么能投清楚交代包含的类型呢?

std::anytype它,但我不能使用这种类型来施放.

例:

#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++ c++17

2
推荐指数
2
解决办法
2762
查看次数