更正友元定义,使std :: map访问私有默认构造函数

时间:2018-05-23 10:14:17

标签: c++ dictionary friend stdmap std-pair

我正在使用一个结构库,该结构应该具有lib用户可以访问的默认构造函数。

struct Example
{
    Example(int x);

  private:
    Example();
};

在库内部,std :: map需要默认构造函数来创建新条目。库在实际上将值放在使用默认构造函数的地方时非常小心。

库使用映射来存储这些结构,如下所示:

std::map<int, Example> data;

检查ideOne中的HERE FOR A COMPLETE EXAMPLE

我想阻止lib的用户能够使用默认构造函数。我如何与std :: map,std :: pair和/或std :: tuple建立联系以允许std :: map使用这个默认构造函数?

friend class std::map<int, Example>;
friend class std::pair<int, Example>;

不起作用,我不知道如何与以下构造函数交朋友抱怨beeing无法访问默认构造函数:

// TEMPLATE CONSTRUCTOR pair::pair(tuple, tuple, sequence, sequence)
template<class _Ty1,
class _Ty2>
template<class _Tuple1,
    class _Tuple2,
    size_t... _Indexes1,
    size_t... _Indexes2> inline
    pair<_Ty1, _Ty2>::pair(_Tuple1& _Val1,
        _Tuple2& _Val2,
        integer_sequence<size_t, _Indexes1...>,
        integer_sequence<size_t, _Indexes2...>)
    : first(_STD get<_Indexes1>(_STD move(_Val1))...),
        second(_STD get<_Indexes2>(_STD move(_Val2))...)
    {   // construct from pair of tuples
    (void) _Val1;   // TRANSITION, VSO#181496
    (void) _Val2;
    }

非常感谢任何支持!

编辑:完整错误消息:

In file included from /usr/include/c++/6/bits/stl_map.h:63:0,
                 from /usr/include/c++/6/map:61,
                 from prog.cpp:2:
/usr/include/c++/6/tuple: In instantiation of ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {int&&}; long unsigned int ..._Indexes1 = {0ul}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const int; _T2 = Example]’:
/usr/include/c++/6/tuple:1579:63:   required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {int&&}; _Args2 = {}; _T1 = const int; _T2 = Example]’
/usr/include/c++/6/ext/new_allocator.h:120:4:   required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, Example>; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, Example> >]’
/usr/include/c++/6/bits/alloc_traits.h:455:4:   required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const int, Example>; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, Example> >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::_Rb_tree_node<std::pair<const int, Example> > >]’
/usr/include/c++/6/bits/stl_tree.h:543:32:   required from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_construct_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, Example>; _KeyOfValue = std::_Select1st<std::pair<const int, Example> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Example> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, Example> >*]’
/usr/include/c++/6/bits/stl_tree.h:560:4:   required from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, Example>; _KeyOfValue = std::_Select1st<std::pair<const int, Example> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Example> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, Example> >*]’
/usr/include/c++/6/bits/stl_tree.h:2196:64:   required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, Example>; _KeyOfValue = std::_Select1st<std::pair<const int, Example> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Example> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const int, Example> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const int, Example> >]’
/usr/include/c++/6/bits/stl_map.h:502:8:   required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = int; _Tp = Example; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Example> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = Example; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]’
prog.cpp:17:8:   required from here
/usr/include/c++/6/tuple:1590:70: error: ‘Example::Example()’ is private within this context
         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
                                                                      ^
prog.cpp:11:2: note: declared private here
  Example(){}
  ^~~~~~~

3 个答案:

答案 0 :(得分:5)

首先,我想纠正你帖子中的重点:

  

在库内部,std :: map

需要默认构造函数

即使T没有默认构造函数,也可以使用std::map<K,T>。见this post。在这种情况下,您无法使用operator[]来阅读&amp;写入地图。您仍然可以使用其他方法:

  • 阅读地图:V value = map.at(key);
  • 插入地图:map.insert(std::make_pair(key, value));

我强烈建议这样做。


话虽如此,如果你真的想要按照“私人构造函数和朋友”的方式,从你的错误信息:

In instantiation of ‘std::pair<_T1, _T2>::pair(/*...*/) [with /*...*/; _T1 = const int; _T2 = Example]’

您可以尝试friend std::pair<const int, Example>;。正如卡莱斯在答案中所说,这可能不是便携式的。

答案 1 :(得分:2)

没有便携式解决方案。

您的类型不是DefaultConstructible,因此尝试data[5]是未定义的行为。

作为参考,我尝试了以下所有内容,g++仍然拒绝了

struct Example
{
    Example(int x) {}

  private:
    Example();
    friend class std::map<int, Example>;
    friend std::map<int, Example>::key_type;
    friend std::map<int, Example>::mapped_type; // warning: class 'Example' is implicitly friends with itself
    friend std::map<int, Example>::value_type;
    friend std::map<int, Example>::size_type;
    friend std::map<int, Example>::difference_type;
    friend std::map<int, Example>::key_compare;
    friend std::map<int, Example>::allocator_type;
    friend std::map<int, Example>::reference;
    friend std::map<int, Example>::const_reference;
    friend std::map<int, Example>::pointer;
    friend std::map<int, Example>::const_pointer;
    friend std::map<int, Example>::iterator;
    friend std::map<int, Example>::const_iterator;
    friend std::map<int, Example>::reverse_iterator;
    friend std::map<int, Example>::const_reverse_iterator;
    friend std::map<int, Example>::node_type;
    friend std::map<int, Example>::insert_return_type;
    friend std::map<int, Example>::value_compare;
};

答案 2 :(得分:1)

不确定这是否是规范性的,但我的GCC接受了:

struct Example 
{
    Example(int x) { }
    private:
    friend class std::pair<int const, Example>;
    Example() { }
};

请注意std::pair<int, Example>将无效,因为std :: map的键是const!