更多关于tempalates [英] more on tempalates

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

问题描述

一些与模板相关的问题:


1.在下面粘贴的代码中,为什么在

中选择基本版本调用foo和在调用bar时选择const版本?


2.使用STL输出迭代器时,是否有任何一般性建议

是否应该是通过价值或参考传递?我有

模板化代码,它带有ostream_iterators或back_insert_iterators

我初步选择通过引用传递,但是我遇到了

类似于下面的情况,我必须复制我的代码以处理

const和每个迭代器类型的非const引用(4基本上是

相同的实现)。


3.与此相关,对于我的代码可以采用

ostream_iterator或back_insert_iterator,有没有办法告诉

编译器应该对这两个进行相同的处理。每个都是输出迭代器的模型

,但我找不到用

语言表达的任何方法。 (注意,代码也可能采用后向插入序列,所以我不能简单地让模板代码假设它的参数是输出

迭代器。 )


关于3,我认为可能最好的方法可能是使用不同的函数

名称输出迭代器版本和后插入序列

版本,让模板假设其中一种型号。


感谢您的建议,

Mark

#include< iostream>

#include< vector>


使用命名空间std;


template< ; typename T>

void foo(T& t){cout<< 基本版本\ n;}}


模板< typename T>

void foo(const vector< T>& t){cout << const version \ n;}}


模板< typename T>

void bar(T t){cout<< 基本版本\ n;}}


模板< typename T>

void bar(const vector< T>& t){cout << " const version \ n";}

int main()

{

vector< int> vi;

foo(vi); //输出:基本版

bar(vi); //输出:const版本

}

A couple template related questions:

1. In the code pasted below, why does the basic version get selected in
the call to foo and the const version get selected in the call to bar?

2. When working with STL output iterators, is there any general advice
on whether they should be passed by value or reference? I have
templated code which takes ostream_iterators or back_insert_iterators
which I inititally chose to pass by reference, but I''ve run into the
situation analogous to below where I have to duplicate my code to handle
const and non-const references to each iterator type (4 basically
identical implementations there).

3. Related to this, for my code which can take either an
ostream_iterator or a back_insert_iterator, is there any way to tell the
compiler that these two should be treated identically. Each is a model
of Output Iterator, but I can''t find any way to express this in the
language. (Note, the code may also take a Back Insertion Sequence so I
can''t simply have the template code assume it''s argument is an Output
Iterator.)

Regarding 3, I think may best approach may be to use different function
names for the Output Iterator version and the Back Insertion Sequence
version, and let the template assume one of these models.

Thanks for your advice,
Mark
#include <iostream>
#include <vector>

using namespace std;

template <typename T>
void foo (T& t) {cout << "basic version\n";}

template <typename T>
void foo (const vector<T>& t) {cout << "const version\n";}

template <typename T>
void bar (T t) {cout << "basic version\n";}

template <typename T>
void bar (const vector<T>& t) {cout << "const version\n";}
int main ()
{
vector<int> vi;
foo(vi); // output: basic version
bar(vi); // output: const version
}

推荐答案



" Mark P <我们**** @ fall2005REMOVE.fastmailCAPS.fm>在消息中写道

新闻:LN ***************** @ newssvr27.news.prodigy.ne t ...

|一对模板相关问题:

|

| 1.在下面粘贴的代码中,为什么选择基本版本

in

|在调用bar时调用foo和const版本会被选中吗?


编译器可以自由选择是否调用矢量模板

或者int模板,因为你没有指定哪个foo< ? >()模板

在main()中使用。在foo()的情况下,它可能会发现向量

模板更有效率,而在bar()的情况下,它可能会计算出传递向量的
复制相当低效。

因此你得到的输出。


note ...缺少参考... T& t

模板< typename T>

void bar(T t){cout<< 复制矢量模板\ n;}}


得到它?你不是在处理一个基本的vs const模板战。

此外,基本vs const战斗不存在。程序员需要告诉编译器是否为常量。否则编译器将会产生一个诊断,它无法确定哪个模板

代更高效或更合适(如果你提供两个

a const和非const版本。


#include< iostream>

#include< vector>


模板< typename T>

void foo(T& t)

{

std :: cout< ;< " foo(),vector template \ n";

}


模板< typename T>

void foo (const std :: vector< T>& t)

{

std :: cout<< " foo(),int template \ n";

}


模板< typename T>

void bar (T t)

{

std :: cout<< " bar(),copy vector template \\\
";

}


template< typename T>

void bar(const std :: vector< T>& t)

{

std :: cout<< " bar(),int template \ n";

}


int main()

{

std :: vector< int> vi;


foo<的std ::矢量< int> >(vi);

foo< int>(vi);

bar<的std ::矢量< int> >(vi);

bar< int>(vi);


返回0;

}


/ *

foo(),矢量模板

foo(),int模板

bar(),复制矢量模板

bar(), int模板

* /


< snip>


"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fm> wrote in message
news:LN*****************@newssvr27.news.prodigy.ne t...
| A couple template related questions:
|
| 1. In the code pasted below, why does the basic version get selected
in
| the call to foo and the const version get selected in the call to bar?

The compiler is free to choose whether it will call the vector template
or the int template since you did not specify which foo< ? >() template
to use in main(). In the case of foo(), it probably found the vector
template to be more efficient and in the case of bar() it probably
calculated that passing the vector via copy considerably less efficient.
Hence the output you got.

note... missing reference... T& t
template <typename T>
void bar (T t) {cout << "copy vector template\n";}

Get it? You aren''t dealing with a basic vs const template fight.
Besides, basic vs const fights don''t exist. A programmer needs to tell
the compiler whether its const or not. Otherwise the compiler will
likely generate a diagnostic where it can''t decide which template
generation is more efficient or appropriate (in the case you supply both
a const and a non-const version).

#include <iostream>
#include <vector>

template <typename T>
void foo (T& t)
{
std::cout << "foo(), vector template\n";
}

template <typename T>
void foo (const std::vector<T>& t)
{
std::cout << "foo(), int template\n";
}

template <typename T>
void bar (T t)
{
std::cout << "bar(), copy vector template\n";
}

template <typename T>
void bar (const std::vector<T>& t)
{
std::cout << "bar(), int template\n";
}

int main ()
{
std::vector<int> vi;

foo< std::vector< int > >(vi);
foo< int >(vi);
bar< std::vector< int > >(vi);
bar< int >(vi);

return 0;
}

/*
foo(), vector template
foo(), int template
bar(), copy vector template
bar(), int template
*/

<snip>


Peter_Julian写道:
Peter_Julian wrote:
" Mark P" <我们**** @ fall2005REMOVE.fastmailCAPS.fm>在消息中写道
新闻:LN ***************** @newssvr27.news.prodigy.ne t ...
|一对模板相关的问题:
|
| 1.在下面粘贴的代码中,为什么在
中选择基本版本
在调用bar时调用foo和const版本会被选中吗?

编译器可以自由选择它是否会调用矢量模板
或int模板,因为你没有指定哪个foo< ? >()模板
在main()中使用。在foo()的情况下,它可能发现矢量
模板更有效,而在bar()的情况下,它可能会计算通过副本传递矢量的效率相当低。
因此你得到的输出。

注意......缺少参考... T& t
模板< typename T>
void bar(T t){cout<< 复制矢量模板\ n"}

获取它?


我不确定。当然有一些规则给出了一个模板的优先级比另一个模板更好,否则

专业化的重点是什么?你是说这些规则无法在每种情况下区分两种选择而且由于我没有,因此编译器选择了另一种是随意的

没有明确指定

模板参数?


- Mark

你没有处理基本的vs const模板战斗。此外,基本与常规斗争不存在。程序员需要告诉编译器是否为const。否则,编译器可能会生成一个诊断,它无法决定哪个模板生成更有效或更合适(在你提供const和非const版本的情况下) )。

#include< iostream>
#include< vector>

模板< typename T>
void foo(T& t )
{
std :: cout<< " foo(),vector template \\\
" ;;
}
模板< typename T>
void foo(const std :: vector< T>& t )
{
std :: cout<< " foo(),int template \\\
" ;;
}

模板< typename T>
void bar(T t)
{
std :: cout<< " bar(),copy vector template \\\
" ;;
}

模板< typename T>
void bar(const std :: vector< T>& t)
{
std :: cout<< " bar(),int template \\\
" ;;
}
int main()
{
std :: vector< int> vi;

foo<的std ::矢量< int> >(vi);
foo< int>(vi);
bar<的std ::矢量< int> >(vi);
bar< int>(vi);

返回0;
}

/ *
foo(),矢量模板
foo() ,int模板
bar(),复制矢量模板
bar(),int模板
* /

< snip>
"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fm> wrote in message
news:LN*****************@newssvr27.news.prodigy.ne t...
| A couple template related questions:
|
| 1. In the code pasted below, why does the basic version get selected
in
| the call to foo and the const version get selected in the call to bar?

The compiler is free to choose whether it will call the vector template
or the int template since you did not specify which foo< ? >() template
to use in main(). In the case of foo(), it probably found the vector
template to be more efficient and in the case of bar() it probably
calculated that passing the vector via copy considerably less efficient.
Hence the output you got.

note... missing reference... T& t
template <typename T>
void bar (T t) {cout << "copy vector template\n";}

Get it?
I''m not sure I do. Surely there are rules which give one template of a
function priority over another, otherwise what''s the point in
specialization at all? Are you saying that those rules are unable to
distinguish between the two choices in each case and it''s arbitrary that
the compiler opts for one over another since I didn''t explicitly specify
the template arguments?

- Mark
You aren''t dealing with a basic vs const template fight. Besides, basic vs const fights don''t exist. A programmer needs to tell
the compiler whether its const or not. Otherwise the compiler will
likely generate a diagnostic where it can''t decide which template
generation is more efficient or appropriate (in the case you supply both
a const and a non-const version).

#include <iostream>
#include <vector>

template <typename T>
void foo (T& t)
{
std::cout << "foo(), vector template\n";
}

template <typename T>
void foo (const std::vector<T>& t)
{
std::cout << "foo(), int template\n";
}

template <typename T>
void bar (T t)
{
std::cout << "bar(), copy vector template\n";
}

template <typename T>
void bar (const std::vector<T>& t)
{
std::cout << "bar(), int template\n";
}

int main ()
{
std::vector<int> vi;

foo< std::vector< int > >(vi);
foo< int >(vi);
bar< std::vector< int > >(vi);
bar< int >(vi);

return 0;
}

/*
foo(), vector template
foo(), int template
bar(), copy vector template
bar(), int template
*/

<snip>



Mark P写道:
2.使用STL输出迭代器时,是否应该按值传递或者是否通过任何一般性建议
参考?


他们应该便宜复制,因此价值很高。

3.与此相关,对于我的代码可以采取
ostream_iterator或者一个back_insert_iterator,有没有办法告诉
编译器这两个应该被同等对待。


这些不是迭代器类别。 ''输入迭代器''或''双向

iterator''是这样的类别....

每个都是输出迭代器的模型,但我不能找到任何方式
用语言表达这一点。


....你似乎知道。现在,通常情况下,除了在函数中使用它的方式之外,你不会表达迭代器

类型。除此之外,你只需要使用

模板< typename InIterator>

void print(InIterator beg,InIterator end)

{...}

即可能选择了这个名称,因此它表示它是一个输入迭代器但是

否则将迭代器类型作为模板参数。

关于3,我认为可能最好的方法可能是使用输出迭代器版本和后插入序列
版本的不同功能名称,并让模板假定其中一个这些模型。
2. When working with STL output iterators, is there any general advice
on whether they should be passed by value or reference?
They should be cheap to copy, therefore by value.
3. Related to this, for my code which can take either an
ostream_iterator or a back_insert_iterator, is there any way to tell the
compiler that these two should be treated identically.
Those are not iterator categories. ''input iterator'' or ''bidirectional
iterator'' are such categories....
Each is a model of Output Iterator, but I can''t find any way
to express this in the language.
....which you seem aware of. Now, typically, you don''t express the iterator
type other than by the way you use it in a function. Other than that, you
only use
template<typename InIterator>
void print( InIterator beg, InIterator end)
{ ... }
i.e. maybe chose the name so it conveys that it is an input iterator but
otherwise leave the the iterator type as a template argument.
Regarding 3, I think may best approach may be to use different function
names for the Output Iterator version and the Back Insertion Sequence
version, and let the template assume one of these models.




我不确定,为什么不使用重载?为什么不使用模板功能?

也许如果你提供了一个例子,你可以得到更好的建议。


Uli


-

常见问题: http: //ma.rtij.nl/acllc-c++.FAQ.html


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

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