正确的友元定义,以授予 std::map 对私有默认构造函数的访问权限

Correct friend definition to give std::map access to private default constructor

本文关键字:默认 构造函数 权限 访问权 访问 map std 定义 友元      更新时间:2023-10-16

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

struct Example
{
Example(int x);
private:
Example();
};

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

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

std::map<int, Example> data;

查看此处以获取 ideOne 中的完整示例。

我想阻止 lib 的用户能够使用默认构造函数。我怎样才能与 std::map、std::p air 和/或 std::tuple 交朋友,以允许 std::map 使用此默认构造函数?

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

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

// 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(){}
^~~~~~~

首先,我想纠正你帖子中的一个重要观点:

在库内部,std::map 需要默认构造函数

即使 T 没有默认构造函数,也可以使用std::map<K,T>。请参阅此帖子。在这种情况下,您不能使用operator[]读取和写入地图。您仍然可以使用其他方法执行此操作:

  • 阅读地图: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>;.正如Caleth在他的回答中所说,这可能不是便携式的。

没有便携式解决方案。

你的类型不是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;
};

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

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

请注意,std::pair<int, Example>不起作用,因为 std::map 的键是 const 的!