c++ - sfinae 与非类型模板参数的概念

出于学术原因,我想实现一个示例,如果非类型模板参数满足给定条件,则选择一个模板。例如,我想要一个只为奇数定义的函数。

可以这样做:

template < int N, bool C = N%2>
struct is_odd: public std::false_type{};
template<int N > 
struct is_odd< N,true >: std::true_type{};

template < int N>
constexpr bool is_odd_v = is_odd<N>::value;

template < int N, typename T=typename std::enable_if_t<is_odd_v<N>, int> >
void Check(){ std::cout << "odd number template specialization " << std::endl; };

int main()
{
   Check<1>();
   Check<2>(); // fails, as expected, ok
}

我认为做这么简单的事情需要很多代码。很清楚,我可以直接在 std::enable_if 中使用取模运算,但假设将对非类型模板参数值进行更复杂的检查。

问:这是否可以在没有这么多间接步骤的情况下完成,但仍然有一些 std::is_xxx 在使用中?

顺便说一句:如果 concepts 可以处理非类型模板参数,它可以做得更简单,但我知道,不是为它设计的......

template < int N >
concept ODD = !(N % 2); 

template < ODD N >
void Check() { std::cout << "odd number template specialization " << std::endl; }

奖励:也许有人知道为什么不为非类型模板参数创建概念?

最佳答案

经过更多的实验工作后,我发现 concepts 可用于非类型模板参数。我在阅读的文档中根本没有找到任何相关内容。

template < int I > 
concept ODD = !(I%2);

template< int N > 
requires( ODD<N> )
void Check() { std::cout << "odd number template spezialisation " << std::endl; }

template< int N > 
requires( !ODD<N> )
void Check() { std::cout << "even number template spezialisation " << std::endl; }

int main()
{
    Check<2>();
    Check<4>();
    Check<3>();
}

Live demo

https://stackoverflow.com/questions/70427057/

相关文章:

pytorch - Pytorch 中软标签的交叉熵

ruby-on-rails - 无法使用 octokit 连接到 Github 帐户

android - 我们如何保存和恢复 Android StateFlow 的状态?

python - 从云函数内部构建容器镜像

ios - FaSTLane Match 环境变量未被 build_app 获取

reactjs - 如何在 React 中使用 map 输出嵌套对象的内容?

java - ThreadPoolExecutor.shutdownNow() 没有在 Thread

fortran - gfortran 与 MKL 的链接导致 'Intel MKL ERROR: P

javascript - Typescript reducer 的 switch case type

angular - Auth0 Angular error during unit test : U