C ++ 11要求分配器是默认构造的,libstdc ++和libc ++不同意? [英] Does C++11 require allocators to be default constructible, libstdc++ and libc++ disagree?

查看:111
本文介绍了C ++ 11要求分配器是默认构造的,libstdc ++和libc ++不同意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用略微修改的 Howard Hinnants的C ++ 11堆栈分配器版本,此处记录这里使用 std :: basic_string 和使用编译 gcc libstdc ++ ,以下示例( 实时查看 ):

  const unsigned int N = 200; 

arena< N>一个;
short_alloc< char,N> ac(a);

std :: basic_string< char,std :: char_traits< char>,short_alloc< char,N&空(ac);

会出现以下错误(以及其他):

 错误:没有匹配函数调用'short_alloc< char,200ul> :: short_alloc()'
if(__n == 0 &&&_; __a == _Alloc())
^

当使用 clang 并使用 libc ++ 查看活动

。)



stdlibc ++ c> c> c>

$

C ++ 11是否需要分配器是默认构造的?哪个实现是正确的?

解决方案

不,C ++ 11不需要一个allocator有默认的构造函数, C ++ 11标准版小程序 17.6.3.5 [allocator.requirements] 包含表 28 分配器要求不包含对默认构造函数的要求,后面的部分提供了最小的兼容接口:


[示例:以下是支持满足表28要求的
最小接口的分配器类模板:

  template< class Tp> 
struct SimpleAllocator {
typedef Tp value_type;
SimpleAllocator(ctor args);

template< class T> SimpleAllocator(const SimpleAllocator< T>& other);

Tp * allocate(std :: size_t n);
void deallocate(Tp * p,std :: size_t n);
};

-end example]



b $ b

它不包含默认构造函数。



有一个 libstdc ++ 错误报告: a href =https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56437 =nofollow> basic_string假设分配器是默认可构造的,它说:


basic_string的空字符串优化假设分配器
是默认可构造的。虽然以前是在C ++ 98中的情况,它
在C ++ 11中不再是真的,因为现在分配器允许有
状态。



考虑附加的示例程序。编译

  g ++ -std = c ++ 11 -c t.cpp 

会生成错误消息,即使它应该编译正常。
问题是_S_construct调用_Alloc(),它不存在



注意,C ++ 11标准不需要默认构造函数。
(第17.6.3.5节,表28)。特别是,Section 17.6.3.5中的SimpleAllocator
示例也会触发相同的错误。



这几乎不是
std :: string中缺少的C ++ 11分配器需求,在我们切换到非COW字符串实现之前不太可能实现


这被固定为 gcc 5.0


修正了GCC 5 )


我们可以使用 wandbox上的gcc 5


Using a slightly modified version of Howard Hinnants's C++11 stack allocator which is documented here and here, with std::basic_string and compiling with gcc which is using libstdc++, the following example (see it live):

const unsigned int N = 200;

arena<N> a;
short_alloc<char, N> ac(a) ;

std::basic_string<char,std::char_traits<char>,short_alloc<char, N>> empty(ac);

gives the following error(amongst others):

error: no matching function for call to 'short_alloc<char, 200ul>::short_alloc()'
   if (__n == 0 && __a == _Alloc())
                       ^

However it works without error when compiling with clang and using libc++ (see it live).

The stdlibc++ implementation of std::basic_string expects the allocator to have a default constructor.

Does C++11 require allocators to be default constructible? Which implementation is correct?

解决方案

No, C++11 does not require an allocator have default constructor, if we look at the draft C++11 standard section 17.6.3.5 [allocator.requirements] it contains Table 28 Allocator requirements which does not contain a requirement for a default constructor and later on in the section a minimal conforming interface is provided:

[ Example: the following is an allocator class template supporting the minimal interface that satisfies the requirements of Table 28:

template <class Tp>
struct SimpleAllocator {
    typedef Tp value_type;
    SimpleAllocator(ctor args );

    template <class T> SimpleAllocator(const SimpleAllocator<T>& other);

    Tp *allocate(std::size_t n);
    void deallocate(Tp *p, std::size_t n);
};

—end example ]

which does not contain a default constructor.

There is a libstdc++ bug report: basic_string assumes that allocators are default-constructible which says:

The empty-string optimization of basic_string assumes that allocators are default constructible. While this used to be the case in C++98, it is no longer true in C++11, as now allocators are allowed to have state.

Consider the attached example program. Compiling with

g++ -std=c++11 -c t.cpp

produces an error message, even though it should compile fine. The problem is the the "_S_construct" calls "_Alloc()", which does not exist.

Note that the C++11 standard does not require default constructors. (Section 17.6.3.5, Table 28). In particular, the SimpleAllocator example from Section 17.6.3.5 would trigger the same bug, too.

and the response was:

This is hardly the only C++11 allocator requirement missing from std::string, ALL of the new requirements are missing, and unlikely to be implemented until we switch to a non-COW string implementation.

This is fixed as of gcc 5.0:

Fixed for GCC 5 (when using the new string ABI)

We can confirm this using gcc 5 on wandbox

这篇关于C ++ 11要求分配器是默认构造的,libstdc ++和libc ++不同意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆