[英]Resulting in an `error: incomplete type` while using std::tuple_size
[英]Incomplete type std::tuple_size<T> even though T is complete?
在下面的代碼中,我嘗試獲取派生自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>>);
}
這會產生以下錯誤:
<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;
|
Inheritance 並不意味着等價。 您的類型type_descriptor<foo>
繼承自std::tuple<int, bool>
,但它不是同一類型。 因此模板參數匹配不會找到它。 如果你想讓它工作,你需要,例如, type_descriptor 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>);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.