gla*_*des 0 c++ incomplete-type stdtuple c++20
在下面的代码中,我尝试获取派生自 的自定义类型的元组大小std::tuple。但编译器抱怨这std::tuple_size是不完整的......我无法真正理解,因为struct foo此时已经明确定义了。自然也应该如此type_descriptor<foo>。这个错误是从哪里来的?
#include <utility>
#include <tuple>
#include <cstdio>
struct foo
{
int a_;
int b_;
};
template <typename T>
struct type_descriptor
{};
template<>
struct type_descriptor<foo>
: std::tuple<int, bool>
{
};
int main(){
printf("Size of tuple = %zu\n", std::tuple_size_v<type_descriptor<foo>>);
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下错误:
<source>:89:27: required from here
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/utility.h:75:61: error: incomplete type 'std::tuple_size<type_declarator<foo> >' used in nested name specifier
75 | inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
|
Run Code Online (Sandbox Code Playgroud)
继承并不意味着等同。您的类型type_descriptor<foo> 继承自 std::tuple<int, bool>,但不是同一类型。因此模板参数匹配将找不到它。如果你想让它工作,你需要,例如,里面的 type_descriptor<foo>类型完全是 std::tuple<int, bool>:
template<>
struct type_descriptor<foo>
{
using type = std::tuple<int, bool>;
};
int main(){
printf("Size of tuple = %zu\n", std::tuple_size_v<type_descriptor<foo>::type>);
}
Run Code Online (Sandbox Code Playgroud)