c++ - boost::any 类型更改导致非法访问

标签 c++ boost shared-ptr

在通过 boost cpp 库一书测试示例时,我注意到更改存储在 boost::any 变量中的类型可能会导致非法访问而不是异常:

好:

boost::any a = 1;
bool *p = boost::any_cast<bool>(&a); // ok, bad cast exception

不好:

boost::any a = std::string { "Boost" };
a = 1;
bool *p = boost::any_cast<bool>(&a); // no exception thrown
std::cout << std::boolalpha << *p << '\n'; // illegal access

因此,我的问题是:这是一个看起来的错误,还是我不知道的与模板使用相关的一些潜在事实?

最佳答案

我明白了the documentation不同的是:

Returns: If passed a pointer, it returns a similarly qualified pointer to the value content if successful, otherwise null is returned. If T is ValueType, it returns a copy of the held value, otherwise, if T is a reference to (possibly const qualified) ValueType, it returns a reference to the held value.

Throws: Overloads taking an any pointer do not throw; overloads taking an any value or reference throws bad_any_cast if unsuccessful.

所以:

  1. 转换的成败取决于存储类型和目标类型。

  2. 不过,失败的表现取决于您是否将指针传递给 any_cast。如果传递一个指针,表现形式就是一个nullptr;否则,表现是一个异常(exception)。

例如考虑这段代码:

#include <boost/any.hpp>
#include <iostream>

int main() {
    boost::any a = 1;

这似乎与您问题中的陈述相矛盾 - 因为它需要一个指针,所以它不会抛出,但指针是 nullptr:

    bool *p = boost::any_cast<bool>(&a); 
    // Prints true
    std::cout << std::boolalpha << (p == nullptr) << std::endl;

这是正常情况下的样子:

    int *q = boost::any_cast<int>(&a); 
    // Prints false
    std::cout << std::boolalpha << (q == nullptr) << std::endl;

这会抛出,因为它不需要指针:

    try {
        boost::any_cast<bool>(a);
    }
    catch(...) {
        std::cout << "caught" << std::endl;
    }

同样适用于字符串存储类型:

    a = std::string { "Boost" }; 
    p = boost::any_cast<bool>(&a); 
    // Prints true
    std::cout << std::boolalpha << (p == nullptr) << std::endl;
    try {
        boost::any_cast<bool>(a);
    }
    catch(...) {
        std::cout << "caught again" << std::endl;
    }
}

关于c++ - boost::any 类型更改导致非法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40019084/

相关文章:

c++ - 有没有办法到达最后的盒子 - GRAPH

c++ - 为什么shared_ptr不能解析函数接口(interface)中的继承关系?

c++ - C++ 中的 shared_ptr 和引用

c++ - 使用 std::shared_ptr 在生产者/消费者线程之间共享数据

c++ - boostscoped_lock 消耗太多CPU

c++ - 内存管理的思路,std::map,boost::shared_ptr

c++ - 标记化和 AST

c++ - 类型转换指针

c++ - 线程中 TCP 套接字的奇怪行为

c++ - 继承一个 std::container 包括它的 boost 序列化