模板技巧? [英] template trick?

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

问题描述

您好!


我有一个模板类,它可以作为

元素的简单向量。


模板< typename T>

class simple_vector:public math_object {

// ...很多simple_vector操作


//麻烦从这里开始:


T sum(){

//计算并返回向量的所有元素的总和

}

};


每当我尝试使用一个将b $ b保持元素的向量时,麻烦就开始了,说,加倍[6]。试图实例化

会产生错误,因为sum()的返回类型变成了一个

数组,这是不可能的。


是否有一种简单的方法让用户拥有

simple_vector< int>的实例sum()操作有意义

同时也允许simple_vector< double [6]>要实例化

并且只有在尝试总结()这些时才会中断?


我希望专业化会解决这个问题,但无论什么时候

我尝试将sum()函数移出模板声明

并将其设为


模板<> simple_vector< int> :: sum(){

}


编译器抱怨该模板不包含

函数叫sum()。但是当然在模板声明中留下一个声明

sum作为T sum()将导致

与sum()不能返回数组相同的错误。


我用谷歌搜索clc ++只是为了开始怀疑我需要功能

专业化而不是模板专业化而且它不能

完成。是这样吗?


注意:这只是一个精简的例子。建议使用

a std :: vector< double>而不是double [6]或者创建一个double6

struct不会解决我的问题。


所以一般来说:我需要的是一个模板,

的一些函数,对于不同的T'需要看起来不同,其中T是

模板typename参数。或者更好 - 我可以为某些T'隐藏一些

模板成员函数,为此

这些函数没有意义/变得非法?


提前感谢,

- J.

Hello!

I have a templated class that serves as a simple vector of
elements.

template <typename T>
class simple_vector : public math_object {
// ... lots of simple_vector operations

// the trouble begins here:

T sum() {
// calculates and returns a sum of all elements of the vector
}
};

The trouble begins whenever I try to have a vector that would
hold elements of, say, double[6]. Trying to instantiate that
gives an error, because the return type of sum() becomes an
array, which is impossible.

Is there a simple way to let the user have instances of
simple_vector<int> for which the sum() operation makes sense
while also allowing simple_vector<double[6]> to be instantiated
and only break upon trying to sum() these?

I was hoping that specialization would fix this, but whenever
I try to move the sum() function out of the template declaration
and make it a

template<> simple_vector<int>::sum() {
}

the compiler complains that the template does not contain a
function called sum(). But of course leaving a declaration of
sum as T sum() within the template declaration will lead to
the same error that sum() can''t return an array.

I''ve googled c.l.c++ only to begin to suspect that I need function
specialization and not template specialization and that it cannot
be done. Is it so?

NB: this is only a stripped-down example. Suggestions to use
a std::vector<double> instead of double[6] or to create a double6
struct won''t fix my problem.

So in general: what I need is a template, a few functions of
which need to look differently for different T''s, where T is the
template typename parameter. Or better -- can I make some
template member function hidden for certain T''s, for which
these functions don''t make sense/become illegal?

thanks in advance,
- J.

推荐答案

在星期二, 2004年5月4日17:24:05 +0200,Jacek Dziedzic

< jacek__NOSPAM __ @ janowo_NO_spam_.net>写道:
On Tue, 04 May 2004 17:24:05 +0200, Jacek Dziedzic
<jacek__NOSPAM__@janowo_NO_spam_.net> wrote:
你好!

我有一个模板化的课程,作为
元素的简单载体。

模板< typename T>
类simple_vector:public math_object {
// ...很多simple_vector操作

//麻烦从这里开始:

T sum(){
//计算并返回向量的所有元素的总和
}
};

每当我尝试有一个矢量,可以保持双重[6]的元素。试图实例化
会产生错误,因为sum()的返回类型变成了一个
数组,这是不可能的。


我认为你是关于double类型元素的SOL [6],因为

它们不可分配或可复制。你不能将东西放入容器中

不可分配/复制。我觉得自己像个白痴一样,因为我花了一个小时或更多的时间玩这个,直到我终于得到一个错误,使得我知道这里有'b $ b'。没有办法可以工作。


也许你可以用存储向量来代替6 / $
双数组?

-leor

是否有一种简单的方法让用户拥有
simple_vector< int>的实例sum()操作有意义
同时还允许simple_vector< double [6]>实例化
并且只有在尝试总结()这些时才会中断?

我希望专业化会解决这个问题,但每当我试图移动sum()函数时从模板声明中取出

模板<> simple_vector< int> :: sum(){
}
编译器抱怨该模板不包含名为sum()的函数。但是当然在模板声明中留下总和作为T sum()的声明将导致
与sum()不能返回数组相同的错误。
我用谷歌搜索clc ++只是为了开始怀疑我需要功能
专业化而不是模板专业化而且它无法完成。是这样吗?

注意:这只是一个精简的例子。建议使用
一个std :: vector< double>而不是双[6]或创建一个double6
结构不会解决我的问题。

所以一般来说:我需要的是一个模板,一些函数
对于不同的T',需要有不同的外观,其中T是
模板typename参数。或者更好 - 我可以为某些T'隐藏一些
模板成员函数,为什么这些函数没有意义/变得非法?

谢谢提前,
- J。
Hello!

I have a templated class that serves as a simple vector of
elements.

template <typename T>
class simple_vector : public math_object {
// ... lots of simple_vector operations

// the trouble begins here:

T sum() {
// calculates and returns a sum of all elements of the vector
}
};

The trouble begins whenever I try to have a vector that would
hold elements of, say, double[6]. Trying to instantiate that
gives an error, because the return type of sum() becomes an
array, which is impossible.
I think you''re SOL with respect to elements of type double[6], because
they''re not assignable or copyable. You can''t put things into containers
that aren''t assignable/copyable. I feel like an idiot, because I''ve spent
an hour or more playing with this, until I finally got an error that made
me realize there''s no way it can ever work.

Perhaps you can make do with storing vectors instead of the arrays of 6
doubles?
-leor


Is there a simple way to let the user have instances of
simple_vector<int> for which the sum() operation makes sense
while also allowing simple_vector<double[6]> to be instantiated
and only break upon trying to sum() these?

I was hoping that specialization would fix this, but whenever
I try to move the sum() function out of the template declaration
and make it a

template<> simple_vector<int>::sum() {
}

the compiler complains that the template does not contain a
function called sum(). But of course leaving a declaration of
sum as T sum() within the template declaration will lead to
the same error that sum() can''t return an array.

I''ve googled c.l.c++ only to begin to suspect that I need function
specialization and not template specialization and that it cannot
be done. Is it so?

NB: this is only a stripped-down example. Suggestions to use
a std::vector<double> instead of double[6] or to create a double6
struct won''t fix my problem.

So in general: what I need is a template, a few functions of
which need to look differently for different T''s, where T is the
template typename parameter. Or better -- can I make some
template member function hidden for certain T''s, for which
these functions don''t make sense/become illegal?

thanks in advance,
- J.




-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java的现场培训, Perl和Unix

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html


" Jacek Dziedzic" < jacek__NOSPAM __ @ janowo_NO_spam_.net>写在消息

news:4097B595.2764F8D9@janowo_NO_spam_.net
"Jacek Dziedzic" <jacek__NOSPAM__@janowo_NO_spam_.net> wrote in message
news:4097B595.2764F8D9@janowo_NO_spam_.net
你好!

我有一个模板化的课程,作为一个简单的矢量
元素。

模板< typename T>
类simple_vector:public math_object {
// ...很多simple_vector操作
//麻烦从这里开始:

T sum()//
//计算并返回向量的所有元素的总和
}
}; <每当我试图拥有一个能够保持双重[6]元素的向量时,麻烦就开始了。试图实例化
会产生错误,因为sum()的返回类型变成了一个
数组,这是不可能的。

是否有一种简单的方法让用户拥有
simple_vector< int>的实例sum()操作有意义
同时还允许simple_vector< double [6]>实例化
并且只有在尝试总结()这些时才会中断?

我希望专业化会解决这个问题,但每当我试图移动sum()函数时从模板声明中取出

模板<> simple_vector< int> :: sum(){
}
编译器抱怨该模板不包含名为sum()的函数。但是当然在模板声明中留下总和作为T sum()的声明将导致
与sum()不能返回数组相同的错误。
我用谷歌搜索clc ++只是为了开始怀疑我需要功能
专业化而不是模板专业化而且它无法完成。是这样吗?
Hello!

I have a templated class that serves as a simple vector of
elements.

template <typename T>
class simple_vector : public math_object {
// ... lots of simple_vector operations

// the trouble begins here:

T sum() {
// calculates and returns a sum of all elements of the vector
}
};

The trouble begins whenever I try to have a vector that would
hold elements of, say, double[6]. Trying to instantiate that
gives an error, because the return type of sum() becomes an
array, which is impossible.

Is there a simple way to let the user have instances of
simple_vector<int> for which the sum() operation makes sense
while also allowing simple_vector<double[6]> to be instantiated
and only break upon trying to sum() these?

I was hoping that specialization would fix this, but whenever
I try to move the sum() function out of the template declaration
and make it a

template<> simple_vector<int>::sum() {
}

the compiler complains that the template does not contain a
function called sum(). But of course leaving a declaration of
sum as T sum() within the template declaration will lead to
the same error that sum() can''t return an array.

I''ve googled c.l.c++ only to begin to suspect that I need function
specialization and not template specialization and that it cannot
be done. Is it so?




如果指针或引用

对所有类型都可接受,则可以返回指针或数组引用。或者,你可以使sum()成为班级的朋友而不是会员。


最后,你可以专注于整个班级,而不是试图

专门用于成员函数。在这种情况下,你必须为你感兴趣的特定类型再次声明和定义

整个类。要做到这一点,你首先要声明普通类。然后你宣布专门的

版本,可能但不需要与一般的

案例有任何共同点,例如,


#include< iostream>


模板< typename T>

class simple_vector

{

T t1,t2;

public:

void print(){std :: cout<< t1<< \\\
; }

总和()

{

返回t1 + t2;

}

};

模板<>

类simple_vector< double [6]>

{

double d [6];

public:

simple_vector()

{

for(int i = 0; i< 6; ++ i)

d [i] = i * i;

}

void print()

{

for(int i = 0; i< 6; ++ i)

std :: cout<< d [1] - ;< ''\ n'';

}

};

int main()

{

simple_vector< double [6]> v;

v.print();

返回0;

}


-

John Carson

1.要回复电子邮件地址,请删除donald

2.不要回复电子邮件地址(在此处发布)



You can return a pointer or reference to an array if a pointer or reference
would be acceptable for all types. Alternatively, you could make sum() a
friend of the class rather than a member.

Finally, you could specialise the entire class rather than attempting to
specialise just a member function. In that case you must declare and define
the entire class again for the particular type you are interested in. To do
this, you first declare the general class. Then you declare the specialised
version, which may but need not have anything in common with the general
case, e.g.,

#include <iostream>

template <typename T>
class simple_vector
{
T t1, t2;
public:
void print() { std::cout << t1 << ''\n''; }
T sum()
{
return t1+t2;
}
};
template <>
class simple_vector<double[6]>
{
double d[6];
public:
simple_vector()
{
for (int i=0; i<6; ++i)
d[i] = i*i;
}
void print()
{
for (int i=0; i<6; ++i)
std::cout << d[i]<< ''\n'';
}
};
int main()
{
simple_vector<double[6]> v;
v.print();
return 0;
}

--
John Carson
1. To reply to email address, remove donald
2. Don''t reply to email address (post here instead)




" Leor Zolman" <乐** @ bdsoft.com>在消息中写道

news:ed ******************************** @ 4ax.com ...

"Leor Zolman" <le**@bdsoft.com> wrote in message
news:ed********************************@4ax.com...
2004年5月4日星期二17:24:05 +0200,Jacek Dziedzic
< jacek__NOSPAM __ @ janowo_NO_spam_.net>写道:
On Tue, 04 May 2004 17:24:05 +0200, Jacek Dziedzic
<jacek__NOSPAM__@janowo_NO_spam_.net> wrote:
你好!

我有一个模板化的课程,作为
元素的简单载体。

模板< typename T>
类simple_vector:public math_object {
// ...很多simple_vector操作

//麻烦从这里开始:

T sum(){
//计算并返回向量的所有元素的总和
}
};

每当我尝试有一个矢量,可以保持双重[6]的元素。试图实例化
会产生错误,因为sum()的返回类型变成了一个
数组,这是不可能的。
Hello!

I have a templated class that serves as a simple vector of
elements.

template <typename T>
class simple_vector : public math_object {
// ... lots of simple_vector operations

// the trouble begins here:

T sum() {
// calculates and returns a sum of all elements of the vector
}
};

The trouble begins whenever I try to have a vector that would
hold elements of, say, double[6]. Trying to instantiate that
gives an error, because the return type of sum() becomes an
array, which is impossible.



我想你是SOL关于double类型的元素[6],因为它们不可分配或可复制。你不能将东西放入不可分配/可复制的容器中。我觉得自己像个白痴,因为我花了一个小时或更长时间玩这个,直到我终于得到了一个错误,让我意识到它无法发挥作用。 br />
也许你可以用存储矢量而不是6 /
的数组来做什么?
-leor



I think you''re SOL with respect to elements of type double[6], because
they''re not assignable or copyable. You can''t put things into containers
that aren''t assignable/copyable. I feel like an idiot, because I''ve spent
an hour or more playing with this, until I finally got an error that made
me realize there''s no way it can ever work.

Perhaps you can make do with storing vectors instead of the arrays of 6
doubles?
-leor




提升有一个固定大小的数组类,特别设计可用于

STL。据推测它也可用于OP代码。

http://www.boost.org/doc/html/array.html

john



boost have a fixed size array class, specifically design to be usable in the
STL. Presumably it would be usable with the OPs code as well.

http://www.boost.org/doc/html/array.html

john


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

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