加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – std :: string的字母到boost :: thread_specific_ptr

发布时间:2020-12-16 09:41:47 所属栏目:百科 来源:网络整理
导读:为什么这不能编译g 4.6和g 4.7? 我试图获得字符串到线程特定存储的映射. 我相信,有一些像这样的东西在提升1.48. 实际上,它与boost的版本无关,但标志-std = c 0x. 如果不存在则编译.所以,寻找错误的解释和 如何解决它. 谢谢 #include map#include boost/thre
为什么这不能编译g 4.6和g 4.7?
我试图获得字符串到线程特定存储的映射.
我相信,有一些像这样的东西在提升1.48.
实际上,它与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,_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,_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,_Alloc>::~_Rb_tree() [with _Key = std::basic_string<char>; _Val = 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;

因为默认构造函数与隐式声明的构造函数没有相同的签名,所以复制构造函数格式不正确.

因此,在两种情况下,对< X,但在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;

所以我会将此报告为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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读