- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
众所周知,在 C++20 中,具有用户声明的构造函数的类不是聚合的。
现在,考虑以下代码:
#include <type_traits>
template <bool EnableCtor>
struct test {
template <bool Enable = EnableCtor, class = std::enable_if_t<Enable>>
test() {}
int a;
};
int main() {
test<false> t {.a = 0};
}
GCC 和 CLang 都不会编译此代码。因此,尽管这里没有实例化构造函数,但该类不是聚合。
构造函数模板是否被视为“声明的构造函数”?标准对这种情况有何规定?
最佳答案
聚合的定义发生了很大变化,但这里的相关部分却相当稳定。 [dcl.init.aggr] 中的 C++20 措辞说:
An aggregate is an array or a class ([class]) with
- no user-declared or inherited constructors ([class.ctor]),
- no private or protected direct non-static data members ([class.access]),
- no virtual functions ([class.virtual]), and
- no virtual, private, or protected base classes ([class.mi]).
请注意,它只是说没有声明的构造函数,句号。关于构造函数对于类模板的特定特化是否可行并不是什么微妙的事情。根本就没有。
因此给出这样的事情:
template <bool B>
struct X {
int i;
X(int i) requires B;
};
X<false>
仍然不是一个聚合,即使它唯一声明的构造函数对于该特化来说是不可行的。没关系。聚合性只是声明的一个属性。
关于c++ - 聚合类和不可行的构造函数模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72032281/
对于我的一个项目,我终于需要使用我的第一个多态类(std::cout 除外)。 我正在研究如何确保至少在某些情况下我有 100% 的去虚拟化调用。 这段代码是否合法可行? dynamic_cast 有
最近有一个编译问题,用这个片段说明: struct Base { }; template struct A : Base { A(){} A(Base&&) {} }; A foo()
注意:这是一个冗长的问题,需要对 MVVM“设计模式”、JSON 和 jQuery 有很好的理解.... 所以我有一个理论/主张 DHTML 中的 MVVM 是可能的 和可行的 并且想知道您是否同意/
我有一台 Mac 服务器,我正在构建 PHP 代码以允许用户上传图像、文档甚至视频文件。研究这个肯定让我很紧张,我希望上传的内容没有病毒。 自己构建一些东西会是一个巨大的挑战吗?您会这样做,还是会
根据文档,ASP.NET 项目(尚)不支持新的 PackageReference https://learn.microsoft.com/en-us/nuget/consume-packages/pa
我是一名优秀的程序员,十分优秀!