std :: is_default_constructible< T>错误,如果构造函数是私有的 [英] std::is_default_constructible<T> error, if constructor is private

查看:93
本文介绍了std :: is_default_constructible< T>错误,如果构造函数是私有的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码段

#include <type_traits>
#include <boost/type_traits.hpp>

class C { C() { } };

int main()
{
   static_assert(!boost::has_trivial_default_constructor<C>::value, "Constructible");
   static_assert(!std::is_default_constructible<C>::value, "Constructible");
}

条件不相等,但第一个条件正常,第二个构造给出错误,该构造函数是私有的。编译器gcc 4.7 ...那么,这是gcc错误还是由标准定义?

Conditions are not equal, but first condition works fine and second construction give error, that constructor is private. Compiler gcc 4.7... So, is this gcc bug, or it's defined by standard?

http://liveworkspace.org/code/NDQyMD $ 5

确定。由于这种条件确实不平等-我们可以使用类似这样的东西

OK. Since this conditions are really unequal - we can use something like this

#include <type_traits>
#include <boost/type_traits.hpp>

class C { private: C() noexcept(false) { } };

int main()
{
   static_assert(!boost::has_nothrow_constructor<C>::value, "Constructible");
   static_assert(!std::is_nothrow_constructible<C>::value, "Constructible");
}

http://liveworkspace.org/code/NDQyMD $ 24

无论如何,我知道static_assert不应失败,因为类型确实不是默认可构造的/不可构造的。 问题是:为什么会有编译错误,而不是由我的静态断言造成?

Anyway, i know, that static_assert should not fails, since types are really not default constructible/not nothrow constructible. Question is: WHY there is compilation error, not by my static assert?

推荐答案

看起来像编译器错误。让我们看一下SFINAE的以下示例(在 g ++ type_traits 标头中使用类似的实现)

Looks like a compiler bug. Let's see the following example of SFINAE(similar implementation is used in g++'s type_traits header)

#include <type_traits>

class Private
{
    Private()
    {

    }
};

class Delete
{
    Delete() = delete;
};

struct is_default_constructible_impl
{
    template<typename T, typename = decltype(T())>
    static std::true_type test(int);

    template<typename>
    static std::false_type test(...);
};

template <class T>
struct is_default_constructible: decltype(is_default_constructible_impl::test<T>(0))
{

};

int main()
{
    static_assert(is_default_constructible<Private>::value, "Private is not constructible");
    static_assert(is_default_constructible<Delete>::value, "Delete is not constructible");
}

第二个断言按预期工作,我们不能推论 Delete 类型,因为该类只有一个已删除的构造函数。但是,当编译器尝试推断 Private()的类型时,会出现错误: Private :: Private()是私有的 。因此,我们有两个带有私有构造函数的类,但是其中一个给出了错误,第二个给出了错误。我认为这种行为是错误的,但是我在标准中找不到确认。

The second assert works as expected, we can't deduce Delete type, since the class has only one deleted constructor. But when compiler tries to deduce type of Private(), it gives error: Private::Private() is private. So, we have two classes with private constructor, but one of them gives error, and second - not. I think, that this behavior is wrong, but I can't find confirmation in the standard.

P.S。所有提供的代码均按clang编译,没有任何错误。

P.S. all off presented codes compiles by clang without any errors.

这篇关于std :: is_default_constructible&lt; T&gt;错误,如果构造函数是私有的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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