我在这里找到了一些词语:http://en.cppreference.com/w/cpp/memory/scoped_allocator_adaptor/construct
所以,我进行了一个简单的测试。if
std::uses_allocator<T, inner_allocator_type>::value==true
(the type T uses allocators, e.g. it is a container)and if
std::is_constructible<T, std::allocator_arg_t, inner_allocator_type, Args...>::value==true
,then calls
std::allocator_traits<OUTERMOST>::construct( OUTERMOST(*this), p, std::allocator_arg, inner_allocator(), std::forward<Args>(args)... );
struct use_arg {
template <typename Alloc>
use_arg(std::allocator_arg_t, Alloc &, int i)
{ std::cout << i << " in use_arg()\n"; }
};
namespace std {
template <typename A> struct uses_allocator<use_arg, A>: true_type {};
} // namespace std
void test_scoped()
{
std::scoped_allocator_adaptor<std::allocator<use_arg>> sa;
auto p = sa.allocate(1);
sa.construct(p, 4);
sa.destroy(p);
sa.deallocate(p, 1);
}
但是gcc和clang给我报了这些错误:https://gist.github.com/anonymous/3e72754a7615162280fb
我还写了use_a
来替换use_arg
,它可以成功运行。
struct use_a {
template <typename Alloc>
use_a(int i, Alloc &) { std::cout << i << " in use_a()\n"; }
};
这些行为是如何发生的?
std::is_constructible<use_a, int,std::scoped_allocator_adaptor<std::allocator<use_a>>>::value
是什么情况?它也是false
但是可以构造。 - linux40<bits/uses_allocator.h>
中第85行的is_constructible
检查和第96行的uses_allocator
检查。 - Jonathan Wakelyis_constructible<T, allocator_arg_t, A, Args...>
,没有检查is_constructible<T, Args..., A>
,对吧?第二个检查是多余的,因为如果为假,则尝试进行该构造将是不合法的,这是标准所要求的。问题在于标准存在缺陷,并且需要执行一个_不是_不合法的_不同_构造。 - Jonathan Wakely