is_constructible和is_destructible不受朋友声明影响 [英] is_constructible and is_destructible unaffected by friend declarations

查看:259
本文介绍了is_constructible和is_destructible不受朋友声明影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当评估 std :: is_constructible friend > std :: is_destructible 。

Clang and GCC appear not to honor friend declarations when evaluating std::is_constructible and std::is_destructible.

关于`is_constructible,cppreference.com说:

Regarding `is_constructible, cppreference.com says:


访问检查的执行方式与上下文无关, Args中的任何类型。只考虑变量定义的直接上下文的有效性。

Access checks are performed as if from a context unrelated to T and any of the types in Args. Only the validity of the immediate context of the variable definition is considered.

(网站不解释 is_destructible 处理访问检查,但访问修饰符会影响 is_destructible 的行为,因此我会预期它的工作方式与 is_constructible 相同。)

(The site doesn't explain how is_destructible deals with access checks, but access modifiers do affect the behavior of is_destructible in general, so I'd expect it to work the same way as is_constructible.)

因此,在我看来, strong>非编译,因为在检查的立即上下文中,构造函数和析构函数可用,如本地变量实例化所示:

Therefore, it seems to me that this code should not compile, since in the immediate context of the check the constructor and destructor are available, as evidenced by the local variable instantiation:

class Private
{
    Private() {}
    ~Private() {}

    friend class Friend;
};

class Friend
{
    public:
        Friend()
        {
            // Both of these should fire, but they do not.
            static_assert(
                !std::is_constructible<Private>::value,
                "the constructor is public");
            static_assert(
                !std::is_destructible<Private>::value,
                "the destructor is public");
            // There is no error here.
            Private p;
        }
};

... Coliru编译它没有错误(使用GCC或Clang)。

...but Coliru compiles it without error (using either GCC or Clang).

这是一个错误(或至少不符合)

It's this a bug (or at least a nonconformity) in both compilers, or is cppreference.com misrepresenting the standard, or am I misunderstanding cppreference.com's statement?

推荐答案

p>这正是


访问检查的执行方式好像是从与 T
Args 中的任何类型。

说。 T 的朋友根据定义不与 T 无关。

says. "A friend of T" is by definition not "unrelated to T".

直接上下文是艺术术语,但无论如何,这句话是在谈论假设变量定义的直接上下文,而不是使用 is_constructible

"immediate context" is a term of art, but in any event the sentence is talking about the immediate context of the hypothetical variable definition, not the use of is_constructible.

is_constructible 检查上下文相关;这意味着相同类型 is_constructible 在不同的上下文中具有不同的基类。

It would be madness to make the is_constructible check context-dependent; that would mean that the same type, is_constructible<T, Args...>, has different base classes in different contexts.

这篇关于is_constructible和is_destructible不受朋友声明影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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