gpt4 book ai didi

c++ - 在模板化结构范围内使用声明模板化

转载 作者:行者123 更新时间:2023-11-28 04:35:50 25 4
gpt4 key购买 nike

<分区>

我已经创建了一个结构来从列表中检索类型,可以这么说。这个版本编译。

template<uint16_t index, class first, class...rest>
struct type_at {

type_at() = delete;

static constexpr inline auto create() {
if constexpr(index == 0) {
struct conditional {
constexpr inline conditional() = default;
using type = first;
}; return conditional();}
else {
struct conditional {
constexpr inline conditional() = default;
using type = typename type_at<index - 1, rest...>::type;
}; return conditional();}
}

using type = typename decltype(create())::type;
};

为了解决实现问题,我制作了一个新版本,将索引模板参数移动到“type”和“create”,就像这样。

template<class first, class...rest>
struct type_at {

type_at() = delete;

template<uint16_t index>
static constexpr inline auto create() {
if constexpr(index == 0) {
struct conditional {
constexpr inline conditional() = default;
using type = first;
}; return conditional();}
else {
struct conditional {
constexpr inline conditional() = default;
using type = typename type_at<rest...>::type<index - 1>;
}; return conditional();}
}

template<uint16_t index>
using type = typename decltype(create<index>())::type;
};

此版本因编译错误而失败 expected ; before < token在 create() 中,我在其中访问结构的类型( ...type<index - 1>; )。

我的问题是:为什么?我不明白为什么编译器不期望模板参数。我试过 type.template,但没有用。我猜我遗漏了一些东西,所以任何帮助理解将不胜感激。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com