功能模板 [英] Function templates

查看:58
本文介绍了功能模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。如果定义了函数模板:


模板< typename A>

A SQ(A& a)

{

返回a * a;

}


如何阻止用户传递一个没有意义的类型,例如

员工类类型?


2.如果员工在下面的模板中作为T传递,那么是否必须定义任务

运算符因为在

函数中有赋值操作?


模板< class T>

void swap(T& x,T& y)

{

T temp;

temp = x;

x = y;

y = temp;

}


3.是" template< class T>"等同于模板< typename T>?


谢谢!

解决方案

< blockquote>

" al" <人*** @ 168.net>在消息新闻中写道:ne *********************** @ bgtnsc04-news.ops.worldnet.att.net ...

1.如果定义了一个函数模板:

如何阻止用户传递一个没有意义的类型,比如
员工类类型?


你不能,你可以阻止他们写作

员工()*员工()

其他比编译器婊子没有为那些

类型定义。

2.如果Employee在下面的模板中作为T传递,那么应该是一个赋值
运算符必须定义,因为在
函数中有赋值操作?


必须有一个可访问。当然,如果用户没有声明一个,则将在一个类中定义operator =



3.是template< class T>等同于模板< typename T>?




是的。 < T类>将匹配任何类型,而不仅仅是类类型。类型名

和类在这种情况下是可以互换的。


" al" <人*** @ 168.net>在消息中写道

news:ne *********************** @ bgtnsc04-news.ops.worldnet.att.net。 ..

1.如果定义了功能模板:

模板< typename A>
A SQ(A& a)
{
返回一个* a;
}

如何阻止用户传递一个没有意义的类型,比如
员工类类型?


让我们试试吧。它不会编译。当然错误信息可能有点

模糊。

2.如果员工在下面的模板中作为T传递,那么作业的运营商是否必须定义,因为在
函数中有赋值操作?

模板< class T>
void swap(T& x,T& y)
{
T temp;
temp = x;
x = y;
y = temp;
}


如果你不会编写一个赋值运算符,编译器将为你提供

只能复制这些位。

3.是template< class T> ;相当于模板< typename T>?


是的。关键字typename这是语言的一个相对较晚的补充。

在这种情况下更有意义的是类。

谢谢!




-

Cy
http://home.rochester.rr.com/cyhome/


al写道:

1.如果定义了功能模板:

模板< typename A>
A SQ(A& a)
{
返回a * a;
}

如何阻止用户传递一个没有意义的类型,比如
员工类类型?




你可以创建如下的专业化:


模板< typename A>

A SQ(A& a)

{

返回a * a;

}


模板<>

员工SQ(员工& a)

{

...一些编译成错误的代码...

}


但是如果你没有提供''运营商*()'',你的原始SQ无论如何都会提供


1. If a function template is defined:

template <typename A>
A SQ(A& a)
{
return a*a;
}

How could prevent users from passing a type which makes no sense, like
Employee class type?

2. If Employee is pass as T in the template below, should an assignment
operator have to be defined since there''re assignment operation in the
function?

template <class T>
void swap(T& x, T& y)
{
T temp;
temp = x;
x = y;
y = temp;
}

3. Is "template <class T>" equivalent to "template <typename T>"?

Thanks!

解决方案


"al" <al***@168.net> wrote in message news:ne***********************@bgtnsc04-news.ops.worldnet.att.net...

1. If a function template is defined:

How could prevent users from passing a type which makes no sense, like
Employee class type?
You can''t, anymore than you can stop them from writing
Employee() * Employee()
other than the compiler bitching about * not being defined for those
types.

2. If Employee is pass as T in the template below, should an assignment
operator have to be defined since there''re assignment operation in the
function?
There has to be one accessible. Of course, operator= will be defined
in a class if the user doesn''t declare one.
3. Is "template <class T>" equivalent to "template <typename T>"?



Yes. <class T> will match any type, not just class types. Typename
and class are interchangeable in this context.


"al" <al***@168.net> wrote in message
news:ne***********************@bgtnsc04-news.ops.worldnet.att.net...

1. If a function template is defined:

template <typename A>
A SQ(A& a)
{
return a*a;
}

How could prevent users from passing a type which makes no sense, like
Employee class type?
Let ''em try. It won''t compile. Of course the error message may be a bit
obscure.

2. If Employee is pass as T in the template below, should an assignment
operator have to be defined since there''re assignment operation in the
function?

template <class T>
void swap(T& x, T& y)
{
T temp;
temp = x;
x = y;
y = temp;
}
If you don''t write an assignment operator the compiler will provide you with
one which just copies the bits.

3. Is "template <class T>" equivalent to "template <typename T>"?
Yes. The keyword "typename" was a relatively late addition to the language.
It makes more sense in this context then "class".

Thanks!



--
Cy
http://home.rochester.rr.com/cyhome/


al wrote:

1. If a function template is defined:

template <typename A>
A SQ(A& a)
{
return a*a;
}

How could prevent users from passing a type which makes no sense, like
Employee class type?



You can create a specialization like:

template <typename A>
A SQ(A& a)
{
return a*a;
}

template <>
Employee SQ(Employee& a)
{
... some code that compiles to an error ...
}

However if you don''t provide a ''operator *()'', your original SQ provides
for an error anyway.


这篇关于功能模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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