gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:21 26 4
gpt4 key购买 nike

在通过 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com