简体   繁体   English

std :: is_default_constructible <T> 错误,如果构造函数是私有的

[英]std::is_default_constructible<T> error, if constructor is private

I have following snippet 我有以下片段

#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");
}

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? 编译器gcc 4.7 ...那么,这是gcc错误,还是由标准定义的?

http://liveworkspace.org/code/NDQyMD $5 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 http://liveworkspace.org/code/NDQyMD $ 24

Anyway, i know, that static_assert should not fails, since types are really not default constructible/not nothrow constructible. 无论如何,我知道,static_assert不应失败,因为类型实际上不是默认可构造的/不可构造的。 Question is: WHY there is compilation error, not by my static assert? 问题是:为什么会有编译错误,不是由我的静态断言引起的?

Looks like a compiler bug. 看起来像一个编译器错误。 Let's see the following example of SFINAE(similar implementation is used in g++ 's type_traits header) 让我们看一下SFINAE的以下示例(在g++type_traits标头中使用了类似的实现)

#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");
}

The second assert works as expected, we can't deduce Delete type, since the class has only one deleted constructor. 第二个assert可以按预期工作,我们不能推断Delete类型,因为该类只有一个已删除的构造函数。 But when compiler tries to deduce type of Private() , it gives error: Private::Private() is private . 但是,当编译器尝试推断Private()类型时,会出现错误: 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. 我认为这种行为是错误的,但是我在标准中找不到确认。

PS all off presented codes compiles by clang without any errors. PS全部提供的代码均由clang编译,没有任何错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们可以假设 std::is_default_constructible<t> 和 std::is_constructible<t> 平等吗?</t></t> - Can we assume std::is_default_constructible<T> and std::is_constructible<T> to be equal? CRTP std :: is_default_constructible无法按预期工作 - CRTP std::is_default_constructible not working as expected `std::default_initializable` 和 `std::is_default_constructible` 有什么区别? - What is the difference between `std::default_initializable` and `std::is_default_constructible`? SFINAE与std :: enable_if和std :: is_default_constructible用于libc ++中的不完整类型 - SFINAE with std::enable_if and std::is_default_constructible for incomplete type in libc++ 是`std :: array <T, 0> `默认可构造的,而`T`不是默认可构造的? - Is `std::array<T, 0>` default constructible where `T` is not default constructible? 初始化std :: array <T,N> 当T不是默认可构造的时,在构造函数初始化器列表中 - Initialization of std::array<T,N> in constructor initializer list when T is not default-constructible std::is_constructible 为私有构造函数返回不一致的值 - std::is_constructible returns inconsistent value for private constructor 如何初始化 std::array<T, n> 如果 T 不是默认可构造的,那么优雅吗? - How to initialize std::array<T, n> elegantly if T is not default constructible? 为什么is_default_constructible <Class> :: value在同一类范围内失败 - Why is_default_constructible<Class>::value fails within the same class scope 平凡的默认可构造std :: optional和std :: variant - trivially default constructible std::optional and std::variant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM