【问题标题】:VS2013 has_nothrow_constructor with protected constructorVS2013 has_nothrow_constructor 带有受保护的构造函数
【发布时间】:2016-05-22 12:22:47
【问题描述】:

在 VS2013 中,has_nothrow_constructor 和 has_nothrow_default_constructor 更接近标准。它现在声明:

> N3797 20.10.4.3 [meta.unary.prop] says that is_nothrow_default_constructible is equivalent to is_nothrow_constructible. Then it says that is_nothrow_constructible requires is_constructible to be true (and additionally the construction must be "known not to throw any exceptions"). Finally, /6 defines is_constructible in terms of "T t(create()...);" being well-formed, and says that "Access checking is performed as if in a context unrelated to T and any of the Args."

在使用模板时,这会成为一个问题,我想基本上测试基类构造函数的抛出能力,它可能受到保护。例如:

namespace
{
    class CMyBase
    {
    protected:
        CMyBase() throw(){};
    };

    template < typename BlockCopyableClass >
    class TestBlockCopyableBase : public BlockCopyableClass
    {
    protected:
        TestBlockCopyableBase() throw(){}
        ~TestBlockCopyableBase() throw(){}
    private:
        static_assert((std::has_nothrow_constructor< BlockCopyableClass >::value), "BlockCopyableBase classes must have nothrow c-tors");
        static_assert((std::has_nothrow_default_constructor< BlockCopyableClass >::value), "BlockCopyableBase classes must have no throw default c-tors");
    };

    class MyBlockCopyable : public TestBlockCopyableBase< CMyBase >
    {
    };
}

这可以在 VS2012/VS2010 上工作,但在 VS2013 上它会返回 false。是否存在不将构造函数可见性纳入等式的检查?

补充说明:CMyBase 类型类有很多,因此对这些类的修改将非常耗时(跨多个团队),并且代码仍需要在 VS2010 上编译。

提前致谢。

【问题讨论】:

  • 我会建议检查noexcept(BlockCopyableClass()),但VS2013不支持它:/

标签: c++ templates c++11 visual-studio-2013 stl


【解决方案1】:

是否存在不将构造函数可见性纳入方程式的检查?

不,没有。 它在 VS2010/2012 中工作的事实可能是一个错误。

has_nothrow_constructor 和 has_nothrow_default_constructor 是 tr1 的遗物(大约 2006 年)。已标准化的功能是is_nothrow_constructible 和is_nothrow_default_constructible。您可能应该使用这些 - 但它们不会给您不同的答案;它们只会让你的代码更便携。

这些都不涉及非公共构造函数。因为构造函数不可访问,所以检查返回 false。类型不是默认可构造的std::is_default_constructible&lt; BlockCopyableClass&gt;::value 是false,因此不能默认构造。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-07
    • 2011-05-30
    • 1970-01-01
    • 2010-12-16
    • 2011-10-23
    • 2021-06-10
    • 2021-10-05
    • 2011-05-02
    相关资源
    最近更新 更多