C++ 类模板和函数模板的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14040329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Class Template and Function Template
提问by Chris_vr
I would like to know the difference between Class Template and Function Template and where should I use each.
我想知道类模板和函数模板之间的区别以及我应该在哪里使用它们。
回答by Mankarse
When instantiated, a class template becomes a class and a function template becomes a function. Examples:
实例化后,类模板变成类,函数模板变成函数。例子:
//Defines a template for a class that can hold two
//objects.
template<typename T1, typename T2>
struct pair {
T1 first;
T2 second;
};
//Defines a template for a function that gives the
//minimum of two values.
template<typename T>
T min(T a, T b) {
return a < b ? a : b;
}
For normal code, you would use a class template when you want to create a class that is parameterised by a type, and a function template when you want to create a function that can operate on many different types.
对于普通代码,当您想要创建一个由类型参数化的类时,您将使用类模板,而当您想要创建一个可以对许多不同类型进行操作的函数时,您将使用函数模板。
Function templates are also able to do type-deduction, which can be useful for creating factory functions:
函数模板还可以进行类型推导,这对于创建工厂函数很有用:
//Deduces the types of T1 and T2, so
//for example, a pair<int, double> can be created
//by `make_pair(10, 1.2)`
template<typename T1, typename T2>
pair<T1, T2> make_pair(T1&& t1, T2&& t2) {
return {std::forward<T1>(t1), std::forward<T2>(t2)};
}
Class templates can be used to write programs that execute at compile-time (using types as values, and template instantiations with pattern matching as pure functions). A simple example of this is this set of class templates which removes all const
from a type:
类模板可用于编写在编译时执行的程序(使用类型作为值,使用模式匹配的模板实例作为纯函数)。一个简单的例子是这组类模板,它const
从类型中删除所有内容:
//Removes all `const` from a type, for example:
//`remove_const_recursive<int const*const volatile>::type`
//is the type `int*volatile`.
template<typename T> struct remove_const_recursive { typedef T type; };
template<typename T> struct remove_const_recursive<T const volatile> {
typedef typename remove_const_recursive<T>::type volatile type;
};
template<typename T> struct remove_const_recursive<T volatile> {
typedef typename remove_const_recursive<T>::type volatile type;
};
template<typename T> struct remove_const_recursive<T const> {
typedef typename remove_const_recursive<T>::type type;
};
template<typename T> struct remove_const_recursive<T&> {
typedef typename remove_const_recursive<T>::type& type;
};
template<typename T> struct remove_const_recursive<T*> {
typedef typename remove_const_recursive<T>::type* type;
};
As you use templates more and more, you will realise that they can be used in a wide variety of ways. Expression templatesallow you to speed up certain types of code or create domain specific languages. Template metaprogrammingand tuples can be used to automatically write a variety of tedious code. You may also realise that the obtuse syntax and limited performance and semantic power of templates means that they have a cost which is not always outweighed by the benefits that they provide.
随着您越来越多地使用模板,您会意识到它们可以以多种方式使用。表达式模板允许您加速某些类型的代码或创建域特定语言。模板元编程和元组可用于自动编写各种繁琐的代码。您可能还意识到模板的钝语法和有限的性能和语义能力意味着它们的成本并不总是被它们提供的好处所抵消。
回答by kenny mccormick
Function templates attempt to deduce the type of specialization from the argument types.
函数模板试图从参数类型推导出特化类型。
Function templates can't be partially specialized while classes can.
函数模板不能部分特化,而类可以。
Function templates can't have default template parameters while classes can.
函数模板不能有默认模板参数,而类可以。