gpt4 book ai didi

C++ 如何将 "overload"模板用于值和类型?

转载 作者:行者123 更新时间:2023-11-30 04:49:34 24 4
gpt4 key购买 nike

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

#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> ... ?

最佳答案

Is there no way to have a more generic thing such as

到目前为止,在任何版本的 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)>);
};

关于C++ 如何将 "overload"模板用于值和类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55350862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com