简体   繁体   English

显式函数模板专业化 - 为什么?

[英]Explicit function template specialization - Why?

I keep reading and researching, different posts, c++ books, articles and so far nobody has explained the rational for this construct to me. 我一直在阅读和研究,不同的帖子,c ++书籍,文章,到目前为止还没有人向我解释这种结构的理性。 It makes no sense and its really bugging me. 它毫无意义,它真的让我烦恼。 The whole point of a template is to parameterize types to functions (or classes, but i'm talking specifically function template, not class). 模板的重点是将类型参数化为函数(或类,但我具体说的是函数模板,而不是类)。 Why use funny template syntax without the type parameter??? 为什么使用没有类型参数的有趣的模板语法???

//this seems ridiculous. why would anybody ever use this?
template<> void Swap(int & a , int & b){}
//I would always use this if I needed to take care of a special case, no?
void Swap(int & a , int & b){}

What am I missing? 我错过了什么? I would really appreciate some insight, and I do understand that function template specialization is not all that useful in practice anyway, but i still want to understand why it was ever invented in the first place. 我真的很感激一些见解,而且我确实理解功能模板专业化在实践中并不是那么有用,但我仍然想要理解为什么它首先被发明。 Whoever came up with it must have had a reason which seemed compelling enough at the time. 想出它的人肯定有一个当时看起来足够引人注目的理由。

Thanks. 谢谢。

Great question! 好问题! Function template specialisation is a bit niche and not generally worth it. 功能模板专业化有点利基,一般不值得。 You might be a bit confused as to the reason though. 你可能会对原因感到有点困惑。

You ask "what's the funny template syntax without a type parameter?" 你问“没有类型参数的有趣的模板语法是什么?” Plenty of use! 充分利用! Specialising templates is very important and useful, and the good old swap example is a classic reason to consider it. 专业化模板非常重要和有用,好的旧swap示例是考虑它的经典理由。 A template embodies the idea of a generic algorithm that works with any type, but often if you know a bit about the type you can drop in a much better algorithm, without calling code needing to know that anything different is happening under the hood. 模板体现了适用于任何类型的通用算法的想法,但通常如果您对类型有所了解,可以使用更好的算法,而不需要调用代码需要知道在引擎盖下发生了任何不同的事情。 Only the compiler knows and it pulls in the best implementation for the real types at the point where the algorithm is instantiated with specific types, so your fast swap happens without the sorting algorithm needing special cases. 只有编译器知道它并且在使用特定类型实例化算法时为实际类型提供最佳实现,因此在没有需要特殊情况的排序算法的情况下进行快速swap Specialisation is a key part of making generic programming useful in the real world (or we'd have to use un-generic versions to get the performance we need). 专业化是使通用编程在现实世界中有用的关键部分(或者我们必须使用非通用版本来获得我们所需的性能)。

Function template specialisation though is a bit niche, for more obscure reasons. 功能模板专业化虽然有点利基,但更为模糊不清。 I guess you've read Herb Sutter's summary ? 我想你已经读过Herb Sutter的总结了吗? So, if you don't want to be caught out, it's a good idea to avoid specialising function templates. 所以,如果你不想被抓住,最好避免专门化功能模板。 ( std::swap is an example though of something you have to specialise rather than overload if you want to be ultra-conformant to the standard. We do this widely in our codebase here and it works well in practice, though overloading would probably work well enough too.) std::swap是一个例子,虽然你需要专注而不是重载,如果你想要超出标准。我们在我们的代码库中广泛地做这个并且它在实践中很好用,虽然重载可能会工作太好了。)

So, please, specialise away all you like. 所以,请专注于你喜欢的一切。 Having class template specialisations, far from being "ridiculous" is often vital, but function template specialisation isn't as useful. 拥有类模板专业化,远非“荒谬”通常是至关重要的,但功能模板专业化并不那么有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM