意外无效地将“sizeof”应用于不完整的类型错误

unexpected invalid application of 'sizeof' to incomplete type error

提问人:mkluwe 提问时间:1/18/2021 最后编辑:Guillaume Racicotmkluwe 更新时间:5/9/2023 访问量:748

问:

在以下代码中

#include <type_traits>

template< typename T >
struct Allocator {
    using element_type = std::aligned_storage_t< sizeof( T ) >;
};

template< typename T >
struct MPT {
    static Allocator< MPT > allocator;
};

using RV = MPT< double >;

template<>
Allocator< RV > RV::allocator = {};

g++ 产生以下错误:将“sizeof”应用于不完整类型“MPT”无效。

我尝试使用 Compiler Explorer (https://godbolt.org/z/ran4or) 使用 g++ 10.2。Clang 和 MSVC 不抱怨(见 https://godbolt.org/z/nsE5Yjhttps://godbolt.org/z/7c7n4e)。

这真的是一个不完整类型的例子吗?哪个编译器在这里是正确的?

C 模板 G++ 语言律师

评论

1赞 StoryTeller - Unslander Monica 1/18/2021
我假设这个例子 godbolt.org/z/cjG4sq 假设这是一个 GCC 错误。显式实例化神奇地使静态成员定义格式正确。在这种情况下,我看不出如何更完整。Allocator<RV>RV
0赞 florestan 1/18/2021
@StoryTeller-UnslanderMonica:但是还没有在定义中定义吗?编译器如何知道当时的大小?MTPMTPMTP
0赞 StoryTeller - Unslander Monica 1/18/2021
@florestan - 它不必知道它。编译器不应完全实例化类型,除非使用它的上下文需要。静态成员可以在声明 wandbox.org/permlink/rdsQmdV8eEDcO1HC 时以不完整类型声明。因此,无需与专用化一起实例化。它可能会被推迟。Allocator< MPT >MPT
0赞 mkluwe 1/18/2021
@StoryTeller-UnslanderMonica 好吧,我需要一位语言律师(检查我的电话簿)。至少,由于您的示例,我似乎有一个解决方法。
0赞 Language Lawyer 1/18/2021
stackoverflow.com/questions/47462707/ 的傻瓜......

答: 暂无答案