Std:: string到boost::thread_specific_ptr的映射
std::map of string to boost::thread_specific_ptr
为什么不能编译g++4.6和g++4.7?我试图得到字符串的映射到线程特定的存储。我相信在1.48的boost中有类似的东西。实际上,它与boost的版本无关,而是与标志-std=c++0x有关。如果不存在,则编译。寻找对误差的解释如何绕过它。
感谢#include <map>
#include <boost/thread/tss.hpp>
#include <boost/shared_ptr.hpp>
int main(int argc, char** argv) {
typedef boost::thread_specific_ptr< int > Tss_int_ptr;
typedef std::map< std::string, Tss_int_ptr > Tss_int_map_t;
Tss_int_map_t tmap;
return 0;
}
错误信息如下。
g++-4.7 -g -std=c++0x -I"/home/someone/open_source/admin/install/boost_1_52_0/include" -c ~/tmp/fail.cpp
In file included from /usr/include/c++/4.7/bits/stl_algobase.h:65:0,
from /usr/include/c++/4.7/bits/stl_tree.h:63,
from /usr/include/c++/4.7/map:60,
from /home/someone/tmp/fail.cpp:1:
/usr/include/c++/4.7/bits/stl_pair.h: In instantiation of ‘struct std::pair<const std::basic_string<char>, boost::thread_specific_ptr<int> >’:
/usr/include/c++/4.7/bits/stl_tree.h:133:12: required from ‘struct std::_Rb_tree_node<std::pair<const std::basic_string<char>, boost::thread_specific_ptr<int> > >’
/usr/include/c++/4.7/bits/stl_tree.h:1082:4: required from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_erase(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type) [with _Key = std::basic_string<char>; _Val = std::pair<const std::basic_string<char>, boost::thread_specific_ptr<int> >; _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char>, boost::thread_specific_ptr<int> > >; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, boost::thread_specific_ptr<int> > >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const std::basic_string<char>, boost::thread_specific_ptr<int> > >*]’
/usr/include/c++/4.7/bits/stl_tree.h:646:9: required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::~_Rb_tree() [with _Key = std::basic_string<char>; _Val = std::pair<const std::basic_string<char>, boost::thread_specific_ptr<int> >; _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char>, boost::thread_specific_ptr<int> > >; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, boost::thread_specific_ptr<int> > >]’
/usr/include/c++/4.7/bits/stl_map.h:90:11: required from here
/usr/include/c++/4.7/bits/stl_pair.h:119:17: error: ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const std::basic_string<char>; _T2 = boost::thread_specific_ptr<int>; std::pair<_T1, _T2> = std::pair<const std::basic_string<char>, boost::thread_specific_ptr<int> >]’ declared to take const reference, but implicit declaration would take non-const
thread_specific_ptr
声明这些成员,以使类不可复制(注意非const形参):
private:
thread_specific_ptr(thread_specific_ptr&);
thread_specific_ptr& operator=(thread_specific_ptr&);
在c++ 03中,std::pair
没有声明复制构造函数,所以如果程序需要,会隐式生成一个。std::pair<X, thread_specific_ptr>
是不可复制的,因为它的一个成员是不可复制的,所以如果使用隐式复制构造函数,它将是一个错误。
在c++ 11中,std::pair
有一个复制构造函数,它是显式默认的。它的签名是:
pair(const pair&) = default;
编译器错误告诉您隐式生成的复制构造函数将具有此签名,因为thread_specific_ptr
复制构造函数签名接受非const引用:
pair(pair&) = default;
由于默认构造函数与隐式声明的构造函数没有相同的签名,因此复制构造函数是病态的。
所以在这两种情况下pair<X, thread_specific_ptr>
都是不可复制的,但在c++ 11中,即使你不尝试复制对象,错误也会很快被发现。
如果boost::thread_specific_ptr
使用正常的c++ 11习语使类不可复制,代码将工作:
thread_specific_ptr(const thread_specific_ptr&) = delete;
thread_specific_ptr& operator=(const thread_specific_ptr&) = delete;
所以我会将此作为bug报告给Boost。在c++ 11模式下,应该删除复制操作。
作为一种解决方法,您可以使用删除的复制操作将该类型包装在自己的类型中,然后使用它:
template<typename T>
struct TSS : boost::thread_specific_ptr<T>
{
TSS() = default;
TSS(void (*f)(T*)) : boost::thread_specific_ptr<T>(f) { }
TSS(const TSS&) = delete;
TSS& operator=(const TSS&) = delete;
};
现在你可以使用这个,你的代码将编译:
typedef TSS< int > Tss_int_ptr;
Boost::thread-specific_ptr
类在某些boost版本中是不可复制的,这意味着它不能在c++03 STL容器中使用。我想这就是问题的根源,也是为什么改变c++0x标志可以修复它。
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- 当两个成员位于同一类中时出错"a nonstatic member reference must be relative to a specific object"
- C++中的指针否定 (!ptr == NULL)
- 从const ptr*转换为ptr*时出现问题
- 这是MSVC 2013中具有共享PTR的单例的正确实现吗?
- 对唯一 ptr 无效读取的引用向量
- C++ 类型转换基础 PTR 到派生 PTR 保存在引用类中
- 如何使用非类型参数传递模板化类的 Ref 或 Ptr