将编译时已知的函数参数转换为std::integral_constant的有效方法

Efficient way to convert a compile time known function argument to a std::integral_constant

本文关键字:integral std 方法 有效 constant 转换 编译 参数 函数      更新时间:2023-10-16

昨天我读了一篇博客文章关于将编译时已知的函数参数从constexpr函数转换为类似std::integral_constant<>的类型。

一个可能的用法示例是从用户定义的字面值转换类型。

考虑下面的例子:

constexpr auto convert(int i)
{
    return std::integral_constant<int, i>{};
}
void test()
{
    // should be std::integral_constant<int, 22>
    using type = decltype(convert(22));
}

但是很明显Clang抛出了以下错误:

error: ‘i’ is not a constant expression return std::integral_constant<int, i>{}; ^

上述博客的作者建议使用模板化的用户定义文字进行分割将数字转换为std::integer_sequence,解析为int型。

但是这个建议似乎对我没用。

是否有一种有效的方法将编译时已知的函数参数转换为像std::integral_constant<>这样的类型?

函数参数永远不能是编译时常量。虽然在我看来这是constexpr的一个设计缺陷,但它就是这样。

可能有其他方法可以做到你想要的(宏,模板),但你不能用函数参数。

你需要使用一个模板:

template <int i>
constexpr auto convert()
{
    return std::integral_constant<int, i>();
}
void test()
{
    // should be std::integral_constant<int, 22>
    using type = decltype(convert<22>());
}

或者(更好的)你可以使用模板别名:

template <int i> using convert = std::integral_constant<int, i>;
void test()
{
    // should be std::integral_constant<int, 22>
    using type = convert<22>;
}