【问题标题】:Omit argument name after computed type在计算类型后省略参数名称
【发布时间】:2019-10-02 18:17:33
【问题描述】:

为什么下面的不能编译

#include <type_traits>

template<class T>
void stuff(T, std::enable_if_t<std::is_trivial_v<T>, int>=0);

虽然这样做

template<class T>
void stuff2(T, int=0);

在计算其类型时似乎需要参数名称。如果我使用 -Wunused-parameter 构建,有没有比 (void)x 技巧更好的解决方案?

实际用例:

template<class SrcType, size_t N>
explicit constexpr ShortString(SrcType const (&src)[N]
                              , std::enable_if_t<(N>=1 && (N - 1 <= npos) && sizeof(SrcType)<=sizeof(value_type)), int> x = 0);

【问题讨论】:

    标签: c++ templates sfinae


    【解决方案1】:

    这里有两个问题:

    1. std::is_trivial_v&lt;T&gt;::value 中有错字 - 删除 _v::value
    2. std::enable_if_t&lt; ... &gt;=0 中的 &gt;= 最终成为单个标记(“大于”符号)。您需要用空格分隔&gt;=

    GCC 对第二个问题相当无益:
    &lt;source&gt;:4:64: error: template argument 2 is invalid

    但 Clang 提供了更多见解:
    &lt;source&gt;:4:62: error: a space is required between a right angle bracket and an equals sign (use '&gt; =')

    【讨论】:

      猜你喜欢
      • 2017-10-28
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 2013-10-06
      • 2021-10-24
      • 2023-03-04
      • 1970-01-01
      • 2011-06-27
      相关资源
      最近更新 更多