c++ - 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++ 中都没有。而且它也不太可能改变。尽管加载了名称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)>);
};
推荐阅读
- javascript - Pascal Case(或 Upper Camel Case),但允许使用 HTMLInputElement 之类的名称?
- python - 使用 Python 访问 Youtube 数据 API 时出错
- r - 如何在零膨胀负二项式模型中预测具有偏移量的新数据
- c - cgo 与使用线程本地存储的 C 库交互
- python - 使用 Python Websockets 库的多线程服务器
- react-native - 排毒错误:层次结构中找不到匹配的视图:(标签值:是“电子邮件”并且视图具有有效的可见性=可见)在 React Native 中
- string - 是否可以使用多个逗号分隔的字符串作为 TypeScript 类型?
- tensorflow - DeepLabV3,珊瑚的分割和分类/检测
- java - 致命异常:java.lang.RuntimeException - 由 java.lang.IllegalStateException 引起。应为 BEGIN_ARRAY,但为 BEGIN_OBJECT
- javascript - 将状态设置器传递给多个组件?