为什么 c++ 中的分配器需要复制构造函数? [英] why does allocator in c++ need a copy constructor?

查看:39
本文介绍了为什么 c++ 中的分配器需要复制构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说这里是因为异常规范.我不明白.这个问题和异常规范有关系吗?

It is said here that it's because of exception specification. I do not understand it. Does this question have any relationship with exception specification?

推荐答案

在通读教程后,我对措辞感到有些困惑.但我相信它就这么简单:本教程解释了为什么分配器的模板标头显示

After reading through the tutorial I was a little confused myself by the wording. But I believe it's as simple as this: the tutorial was explaining why the allocator's template header shows

allocator(const allocator&) throw();

模板allocator(const allocator<U>&) throw();

即使复制构造函数对于分配器来说是相当无用的.答案是分配器的规范不允许构造函数抛出异常.因此,复制构造函数公共接口定义了带有 throw() 异常规范的复制构造函数(不抛出任何异常),以防止有人使用可能抛出异常的复制构造函数派生自己的分配器.

even though the copy constructor is fairly useless for an allocator. And the answer was that the specification of an allocator does not allow the constructor to throw exceptions. Therefore the copy constructor public interface defines copy constructors with an exception specification of throw() (does not throw any exceptions) to prevent someone deriving their own allocator with copy constructors which might throw an exception.

参见 这个链接 很好地描述了异常规范是什么,如果这就是抛出你的原因.(没有双关语.真的.)

See this link for a good description of what an exception specification is if that's what was throwing you. (No pun intended. Really.)

所以,它们并不意味着在创建分配器时,您必须提供复制构造函数.他们只是指出规范明确禁止您定义抛出任何异常的规范.`

So, they didn't mean that when creating an allocator, you have to provide a copy constructor. They just were pointing out that the specification specifically prohibits you from defining one that throws any exceptions. `

这篇关于为什么 c++ 中的分配器需要复制构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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