C++ Befriending boost::ptr_map / boost::checked_delete fails
C++ Befriending boost::ptr_map / boost::checked_delete fails
我想在存储自身实例的特定类中使用boost::ptr_map。但是,请考虑以下示例:
#include <boost/checked_delete.hpp>
#include <boost/ptr_container/ptr_map.hpp>
class foo
{
friend void boost::checked_delete<>(foo*);
~foo() {}
};
int main()
{
boost::checked_delete(new foo); // OK
boost::ptr_map<int, foo> foo_map; // error C2248: 'foo::~foo' : cannot access private member declared in class 'foo'
return 0;
}
错误发生在以下行
// verify that types are complete for increased safety
template<class T> inline void checked_delete(T * x)
{
// intentionally complex - simplification causes regressions
typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
(void) sizeof(type_must_be_complete);
delete x; // error C2248
}
这里到底发生了什么?它不应该起作用吗?我认为问题在于,模板是在包含它们的编译单元中定义的,boost::checked_delete是从bosst::ptr_map实现源中的另一个编译单元调用的。所以,这与我作为朋友声明的函数不同。
但是,有解决这个问题的方法吗?
在声明友元时尝试以下语法:
template <class T>
friend void boost::checked_delete(T*);
这是来自GCC的巨大错误消息*的开始,它是实例化链的开始(通常,在这种情况下):
在main.cpp:1:0:中包含的文件中:main.cpp:在函数"void boost::checked_delete(T*)[with T=const foo]"中:
添加
friend void boost::checked_delete<>(foo const*);
使代码可编译。
(*):13行3510个字符,270个字符/行
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何在boost beast http请求中设置http头
- Boost Spirit,获取迭代器内部语义动作
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- boost::asio如何生成多个协同程序,然后加入它们
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 使用Boost Interprocess创建托管共享内存需要很长时间
- Boost::posix_time::ptime舍入到给定的分钟数
- boost xml parsingl将xml的路径作为变量发送