Questions tagged «object-identity»

2
[[no_unique_address]]和两个相同类型的成员值
我玩弄[[no_unique_address]]在c++20。 在cppreference的示例中,我们有一个空类型Empty和Z struct Empty {}; // empty class struct Z { char c; [[no_unique_address]] Empty e1, e2; }; 显然,的大小Z至少必须是2因为e1和的类型e2相同。 但是,我真的很想拥有Zsize 1。这让我开始思考,如何Empty在包装类中使用额外的模板参数来包装,这些模板参数会强制执行e1和类型的不同e2。 template <typename T, int i> struct Wrapper : public T{}; struct Z1 { char c; [[no_unique_address]] Wrapper<Empty,1> e1; [[no_unique_address]] Wrapper<Empty,2> e2; }; 不幸的是,sizeof(Z1)==2。是否有使大小Z1成为一体的技巧? 我有测试此gcc version 9.2.1与clang version 9.0.0 在我的应用程序中,我有很多空类型的表格 …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.