正确的友元定义,以授予 std::map 对私有默认构造函数的访问权限
Correct friend definition to give std::map access to private default constructor
我正在开发一个库,该库使用的结构不应具有库用户可访问的默认构造函数。
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 的!
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 初始化具有非默认构造函数的std::数组项的更好方法
- 具有默认模板类型的默认构造函数的类型推导
- 如何使用非默认构造函数实例化模板化类
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 声明没有默认构造函数的字段
- 没有默认构造函数作为模板参数的自定义比较器
- C++17 没有默认构造函数的地图放置(私有默认构造函数)
- 使用移动调用对等构造函数unique_ptr默认构造函数
- C++复制构造函数和默认构造函数
- 将向量从 N1 缩小到 N2 项,而不触发默认构造函数并仅使用 move 语义
- 为什么即使我调用参数化构造函数也会调用默认构造函数?
- 具有非默认构造函数的单例类
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 如何处理没有默认构造函数但在另一个构造函数中构造的对象?
- 在C++中使用默认构造函数初始化对象的不同方法
- 在没有默认构造函数的情况下创建的派生对象
- 强制使用默认构造函数对成员进行未初始化的声明
- 使用默认构造函数初始化对象的不同方法
- 创建类类型的动态分配数组,其中类不得具有默认构造函数