【问题标题】:Matching std::index_sequence by default argument默认参数匹配 std::index_sequence
【发布时间】:2019-05-29 08:10:34
【问题描述】:

模板参数推导允许以下内容还是未推导的上下文?

#include <utility>
#include<tuple>

template<std::size_t... I>
auto make(std::index_sequence<I...> = std::make_index_sequence<2>())
{
    return;
}

int main() {
    make();
}

Compile warning is pretty weird

【问题讨论】:

  • 编译器警告告诉你它“推导出”一个空的I...,因此它不能将std::index_sequence&lt;0, 1&gt;转换为std::index_sequence&lt;&gt;

标签: c++ c++17 template-meta-programming template-argument-deduction


【解决方案1】:

默认参数不是演绎的一部分。

所以你不能在这里做你想做的事(那样)。

如果合适,你可以这样做:

template <typename Seq = std::index_sequence<0, 1>>
auto make(Seq = std::make_index_sequence<2>())
{
    /*...*/
}

【讨论】:

  • 没有扩展包有点达不到目的。
  • @PasserBy:转发到辅助函数(无默认值)可能会成功。
【解决方案2】:

对于它的价值,这是编译:

template<std::size_t... I>
auto make(std::index_sequence<I...> = std::make_index_sequence<sizeof... (I)>())

但可能不会做你想做的事。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-29
    • 2016-12-13
    • 2021-10-27
    • 2018-10-15
    • 2022-01-02
    • 2021-12-27
    • 1970-01-01
    • 2021-07-24
    相关资源
    最近更新 更多