何时删除模板实例化而不是删除非模板超载? [英] When is deleting a template instantiation preferable to deleting a non-template overload?

查看:92
本文介绍了何时删除模板实例化而不是删除非模板超载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个使用原始指针的模板:

Suppose I have a template that works with raw pointers:

template<typename T>
void processPointer(T* ptr);

我不想使用 void * 指针。看来我有两个选择。我可以删除一个非模板的重载:

I don't want this to be called with void* pointers. It seems I have two choices. I can delete a non-template overload:

void processPointer(void*) = delete;

或者我可以删除模板实例化:

Or I can delete a template instantiation:

template<>
void processPointer<void>(void*) = delete;

声明非模板重载更容易(不用尖括号括起来)。是否有理由为什么我想要删除模板实例化?

Declaring the non-template overload is easier (no futzing with angle brackets). Are there reasons why I'd prefer to delete the template instantiation instead?

推荐答案

这里有一个原因喜欢模板版本: processPointer< void>(void *)直接,避免其他重载。

Here's one reason to favor the template version: processPointer<void>(void*) can still be invoked directly, avoiding the other overload.

这篇关于何时删除模板实例化而不是删除非模板超载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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