簡體   English   中英

不完整類型 std::tuple_size<t> 即使 T 是完整的?</t>

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM