首页 > 解决方案 > C ++如何“重载”模板以同时用于值和类型?

问题描述

考虑这个简单的元查询来检查积分:

#include <type_traits>

template <typename T>
struct IsIntegralConstant
{
    static constexpr bool value = std::is_integral_v<T>;
};

// partial specialization if type destructuring matches an integral_constant
template <typename T, auto N>
struct IsIntegralConstant<std::integral_constant<T, N>>
{
    static constexpr bool value = true;
};
template <typename T> constexpr auto isIntegralConstant_v = IsIntegralConstant<T>::value;

// Now I can do queries such as:
static_assert( isIntegralConstant_v<int> );
static_assert( isIntegralConstant_v<std::integral_constant<int, 2>> );

// it'd be nice if it could work for direct values too 
// static_assert( isIntegralConstant_v<2> ); ?

// let's see...
template <auto N>
struct IsIntegralConstant<std::integral_constant<decltype(N), N>>
{
    static constexpr bool value = true;
};
// gcc:
 //error: partial specialization of 'struct IsIntegralConstant<std::integral_constant<decltype (N), N> >' after instantiation of 'struct IsIntegralConstant<std::integral_constant<int, 2> >' [-fpermissive]
 //struct IsIntegralConstant<std::integral_constant<decltype(N), N>>
// what ?

// clang says nothing, until we force instanciation:
static_assert( isIntegralConstant_v<2> );

//#1 with x86-64 clang 8.0.0
//<source>:35:38: error: template argument for template type parameter must be a type

玩它:https ://godbolt.org/z/dosK7G

似乎当您的主要模板在类型或值之间做出决定时,您就会被所有专业化所困扰。

有没有办法有更通用的东西,例如:

template <autotypename TorV> ... ?

标签: c++templatesmetaprogrammingtemplate-auto

解决方案


有没有办法有一个更通用的东西,比如

到目前为止,在任何版本的 C++ 中都没有。而且它也不太可能改变。尽管加载了名称IsIntegralConstant,但我们显然不会测试IsIntegralConstant_v<2>. 模板的用处在于通用代码,这里什么都不缺:

template<auto wut>
struct bar {
     // static_assert(IsIntegralConstant_v<wut>);
     // ill-formed, but we only really care and need
     static_assert(IsIntegralConstant_v<decltype(wut)>);
};

推荐阅读