为什么在Visual C ++ 2012中将bad_alloc(const char *)设为私有? [英] Why the bad_alloc(const char*) was made private in Visual C++ 2012?

查看:93
本文介绍了为什么在Visual C ++ 2012中将bad_alloc(const char *)设为私有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想使用Visual Studio 2012 Release Candidate C ++编译一个更大的项目。现在,该项目已使用VS2010进行了编译。 (我很贪婪地得到C ++ 11的东西,所以我尝试了。:)

I am just trying to compile a bit bigger project using the Visual Studio 2012 Release Candidate, C++. The project was/is compiled using the VS2010 now. (I am just greedy to get the C++11 things, so I tried. :)

除了我自己可以解释的部分内容外,项目使用了代码像这样:

Apart of things that I can explain by myself, the project uses the code like this:

ostringstream ostr;
ostr << "The " __FUNCTION__ "() failed to malloc(" << i << ").";
throw bad_alloc(ostr.str().c_str());

编译器现在抱怨

error C2248: 'std::bad_alloc::bad_alloc' : cannot access private member declared 
    in class 'std::bad_alloc'

...是的。该版本的构造函数现在是私有的。

... which is true. That version of constructor is now private.

将那个版本的构造函数设为私有的原因是什么? C ++ 11标准是否建议不要在参数中使用该构造函数?

What was the reason to make that version of constructor private? Is it recommended by C++11 standard not to use that constructor with the argument?

(我可以想象如果分配失败,可能会导致更多问题,

(I can imagine that if allocation failed, it may cause more problems to try to construct anything new. However, it is only my guess.)

谢谢,
Petr

Thanks, Petr

推荐答案

C ++ 11标准将 bad_alloc 定义为(18.6.2.1):

The C++11 Standard defines bad_alloc as such (18.6.2.1):

class bad_alloc : public exception {
public:
    bad_alloc() noexcept;
    bad_alloc(const bad_alloc&) noexcept;
    bad_alloc& operator=(const bad_alloc&) noexcept;
    virtual const char* what() const noexcept;
};

没有带字符串的构造函数。提供此类构造函数的供应商将使使用它的代码不可移植,因为其他供应商没有义务提供它。

With no constructor that takes a string. A vendor providing such a constructor would make the code using it not portable, as other vendors are not obliged to provide it.

C ++ 03标准定义了类似的集合构造函数,因此VS甚至在C ++ 11之前都没有遵循标准的这一部分。 MS确实尝试使VS尽可能符合标准,因此他们可能只是利用了这种情况(新VS,新标准)来解决不兼容问题。

The C++03 standard defines a similar set of constructors, so VS didn't follow this part of the standard even before C++11. MS does try to make VS as standard compliant as possible, so they've probably just used the occasion (new VS, new standard) to fix an incompatibility.

编辑:现在,我已经看到了VS2012的代码,很明显为什么提到的构造函数是私有的,而不是被完全删除:在<$ c中似乎只有一种使用该构造函数的方法$ c> bad_array_new_length 类。因此 bad_array_new_length 在bad_alloc中被声明为 friend ,因此可以使用该私有构造函数。如果 bad_array_new_length 只是将消息存储在 what()使用的指针中,则可以避免这种依赖性。反正很多代码。

Now that I've seen VS2012's code, it is also clear why the mentioned constructor is left private, instead of being completely removed: there seems to be only one use of that constructor, in the bad_array_new_length class. So bad_array_new_length is declared a friend in bad_alloc, and can therefore use that private constructor. This dependency could have been avoided if bad_array_new_length just stored the message in the pointer used by what(), but it's not a lot of code anyway.

这篇关于为什么在Visual C ++ 2012中将bad_alloc(const char *)设为私有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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