gpt4 book ai didi

c++ - is_constructible 和 is_destructible 不受 friend 声明的影响

转载 作者:可可西里 更新时间:2023-11-01 17:56:37 30 4
gpt4 key购买 nike

在评估 std::is_constructiblestd::is_destructible 时,Clang 和 GCC 似乎不遵守 friend 声明。

关于`is_constructible,cppreference.com says:

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 相同。)

因此,在我看来这段代码应该编译,因为在检查的直接上下文中,构造函数和析构函数可用,正如局部变量实例化所证明的那样:

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 compiles it without error (使用 GCC 或 Clang)。

这是 两个 编译器中的错误(或至少是不一致),还是 cppreference.com 歪曲了标准,或者我误解了 cppreference.com 的声明?

最佳答案

原来是这样

Access checks are performed as if from a context unrelated to T and any of the types in Args.

说。 “T 的 friend ”根据定义并非“与T 无关”。

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

制作 is_constructible 将是疯狂的检查上下文相关;这意味着相同的类型,is_constructible<T, Args...> , 在不同的上下文中有不同的基类。

关于c++ - is_constructible 和 is_destructible 不受 friend 声明的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443705/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com