Std:: string到boost::thread_specific_ptr的映射

std::map of string to boost::thread_specific_ptr

本文关键字:specific ptr 映射 thread string boost Std      更新时间:2023-10-16

为什么不能编译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标志可以修复它。