使用enable_if进行部分模板功能的专门化:设置为默认实现 [英] Partial template function specialization with enable_if: make default implementation

查看:50
本文介绍了使用enable_if进行部分模板功能的专门化:设置为默认实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++ 11的 enable_if ,我想为一个函数(例如,基于参数的类型)以及默认实现定义几种专门的实现.定义它的正确方法是什么?

Using C++11's enable_if I want to define several specialized implementations for a function (based on the type of the parameter, say) as well as a default implementation. What is the correct way to define it?

下面的示例无法正常工作,因为无论类型为 T ,都将调用通用"实现.

The following example does not work as intended since the "generic" implementation is called, whatever the type T.

#include <iostream>

template<typename T, typename Enable = void>
void dummy(T t)
{
  std::cout << "Generic: " << t << std::endl;
}


template<typename T, typename std::enable_if<std::is_integral<T>::value>::type>
void dummy(T t)
{
  std::cout << "Integral: " << t << std::endl;
}


template<typename T, typename std::enable_if<std::is_floating_point<T>::value>::type>
void dummy(T t)
{
  std::cout << "Floating point: " << t << std::endl;
}

int main() {
  dummy(5); // Print "Generic: 5"
  dummy(5.); // Print "Generic: 5"
}

在我的最小示例中,一种解决方案是使用

One solution in my minimal example consists in explicitly declaring the "generic" implementation as not for integral nor floating point types, using

std::enable_if<!std::is_integral<T>::value && !std::is_floating_point<T>::value>::type

这正是我要避免的事情,因为在我的实际用例中,有很多专门的实现,并且我想避免默认实现的漫长(容易出错!)条件.

This is exactly what I want to avoid, since in my real use cases there are a lot of specialized implementations and I would like to avoid a very long (error prone!) condition for the default implementation.

推荐答案

功能不能部分专门化.我假设您想做的是选择那些包含显式条件的重载?一种实现方法是通过在 default 函数的声明中使用可变参数省略号,因为省略号函数在重载解析顺序中具有较低的优先级:

Function cannot be partially specialized. I assume what you want to do is to prefer those overloads which contains explicit condition? One way to achieve that is by using variadic arguments ellipsis in declaration of the default function as the ellipsis function have lower priority in overload resolution order:

#include <iostream>

template<typename T>
void dummy_impl(T t, ...)
{
  std::cout << "Generic: " << t << std::endl;
}


template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
void dummy_impl(T t, int)
{
  std::cout << "Integral: " << t << std::endl;
}


template<typename T, typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
void dummy_impl(T t, int)
{
  std::cout << "Floating point: " << t << std::endl;
}

template <class T>
void dummy(T t) {
   dummy_impl(t, int{});
}

int main() {
  dummy(5); 
  dummy(5.); 
  dummy("abc"); 
}

输出:

Integral: 5
Floating point: 5
Generic: abc

[实时演示]

在注释中提到的@doublep的另一个选项是通过在实现函数的过程中使用结构,然后对其进行部分专业化处理.

Another option as @doublep mention in comment is by use of structure with implementation of your function and then partially specialize it.

这篇关于使用enable_if进行部分模板功能的专门化:设置为默认实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆