来自cppreference。
template< class Alloc >
的用途是什么?对于第一个问题,我尝试了各种各样的方法,但我不能正确。例如
std::stack<int> first;
first.push(1);
first.push(2);
std::stack<int> second {first, std::allocator<int>()}; // error
对于第二个问题,我不明白成员模板构造函数template< class Alloc>
的目的是什么。例如,vector有一个构造函数vector( const vector& other, const Allocator& alloc );
,它清楚地表明第二个参数是一个分配器,并且它可以初始化为简单如
std::vector<int> first {1, 2, 3};
std::vector<int> second {first, std::allocator<int>()};
答案 0 :(得分:3)
像T.C.嫌疑人,看起来libstdc ++还没有实现那些构造函数。这是来自他们的doxygen的source:
128 #if __cplusplus < 201103L
129 explicit
130 stack(const _Sequence& __c = _Sequence())
131 : c(__c) { }
132 #else
133 explicit
134 stack(const _Sequence& __c)
135 : c(__c) { }
136
137 explicit
138 stack(_Sequence&& __c = _Sequence())
139 : c(std::move(__c)) { }
140 #endif
另一方面,我的Clang的片段包括:
template <class _Alloc>
_LIBCPP_INLINE_VISIBILITY
stack(const container_type& __c, const _Alloc& __a,
typename enable_if<uses_allocator<container_type,
_Alloc>::value>::type* = 0)
: c(__c, __a) {}
template <class _Alloc>
_LIBCPP_INLINE_VISIBILITY
stack(const stack& __s, const _Alloc& __a,
typename enable_if<uses_allocator<container_type,
_Alloc>::value>::type* = 0)