English 中文(简体)
为什么我们需要C++的模板关键字?
原标题:Why we need the template keyword in C++?

函数模板 :

template<class T> T 
max(T a, T b){return (a > b)? a: b;}

使用时:

max<int>(a, b); // Yeah, the "<int>" is optional most of the time.

但如果您允许, 我们可以这样写模板 :

T max<class T>(T a, T b){return (a > b)? a: b;} 
//I know the return type T is not in its scope, don t focus on that.

这样我们就可以保持同样的声明形式, 并使用和正常功能一样的功能。 甚至不需要引入和输入关键字“ 模板 ” 。 我认为类模板是一样的 。 那么, 是否还有其他理由让模板成为我们今天所知道的形式?

i 更改了表单,使您不关注返回类型:

auto max<class T>(T a, T b) -> T {return (a > b)? a: b;}
//This is C++11 only and ugly i guess. 
//The type deduce happens at compile time 
//means that return type really didn t to be a problem.
最佳回答

The immediate answer that comes to my mind is:
Just because someone who laid out the proposal for templates said so and no one on the standards committee felt that typing those extra 8 characters would be an overhead.

On a different note:
The syntax of templates is complicated and intimidating to begin with, making sure of presence of the keyword template makes it more intuitive to a reader of code that they are dealing with templates and not any of the other beasts provided by C++ or any implementation specific constructs(read compiler extensions).

问题回答

您在使用前必须声明 < code> T , 所以它真的必须 < 强 > >

<class T> T max(T a, T b){return (a > b)? a: b;} 

但它又不清楚什么是 - 编纂者很可能对此感到困惑。 前方的 template 清楚显示 不是操作者,而是包含类型声明的括号 。

从这个角度看,你的第二个例子应该是可能的,但是要记住,只有C++11才可能采用这种语法,而且模版早就采用了。

我意识到问题在于为了便于编译者实施:在C++中使用的任何名字都必须至少申报,然后才能用于帮助编译者从一开始就能够解析(因为我不知道原因 ) 。 这就是你有一个奇怪的新语法可以声明函数的原因之一,它允许在参数之后定义返回类型。

因此,这里的理由是,阅读您的例子, T 是使用的第一个名字, 但是它之前没有被宣布, 所以编译者不知道它是什么, 或者它是什么表达方式 。

很快地,你就会在这种方式下 迅速陷入问题分析的境地:

template <typename> int f(); // Current declaration syntax, type template argument.
template <int> int f();      // Current declaration syntax, non-type template argument.

void f<class>(); // New declaration syntax, type argument. 
void f<int>(); // New declaration syntax, non-type argument.
(void) f<int>(); // (void) is a cast , f is instantiation of f<class> with type int.




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签