【问题标题】:Iterate tuple types [closed]迭代元组类型
【发布时间】:2020-04-01 10:28:37
【问题描述】:

当尝试使用以下代码迭代 std::tuple 类型时,我在 clang 和 gcc 中遇到编译器错误: error: implicit instantiation of undefined template 'std::tuple_element<0, std::tuple<int, float, char> >'

MSVC 编译正常。

#include <array>
#include <iostream>

template <typename Tuple, size_t Index = 0>
inline void iterate_tuple_types() {
    using Element = typename std::tuple_element<Index, Tuple>::type;
    if constexpr (Index + 1 < std::tuple_size<Tuple>::value) {
        iterate_tuple_types<Tuple, Index + 1>();
    }
}

int main() {
    using t = std::tuple<int, float, char>;
    iterate_tuple_types<t>();
}

根据 c++20 标准,哪个编译器是正确的?

【问题讨论】:

    标签: c++ templates tuples stdtuple


    【解决方案1】:

    你忘了#include &lt;tuple&gt;

    【讨论】:

    • 你对你说“你”吗? ;-)
    • 有时。对于那些在搜索相同的编译器错误后可能会发现此答案有用的人。但是我看到这里不欢迎这样的社区帮助。
    • 对不起,骗了你。我的印象是在提到提问者时通常使用OP(原始海报)。顺便提一句。这可以规避“他/她”的问题,这可能会让某些人感到不舒服。 self-answer 没有任何问题,但在这种情况下,我忘了#include &lt;tuple&gt; 也应该没问题。 ;-)
    • @cos 它普遍受到欢迎,但我不确定这个具体案例。我们真的需要一个单独的问答来忘记每个特定的标题吗?
    • 我以前在 SO 上发现这些琐碎的答案很有用,所以我想把它留给以后的搜索。因为这个特定的编译器错误描述性不够。您可以在相关书籍中找到有关基本问题的基本答案。 SO 非常适合解决实际的日常问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多