Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

template function (vector<int> vs. vector<gsl_vector*>)

4 views
Skip to first unread message

zl2k

unread,
Aug 17, 2010, 8:18:24 PM8/17/10
to
hi, there,
I have
void Foo1(vector<int>& v){
....
A.process(v[i]);
....
}

void Foo2(vector<gsl_vector*>& v){
....
A.process(v[i]);
....
}

How can I integrate Foo1 and Foo2 into a template function? Thanks.

zl2k

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Stuart Golodetz

unread,
Aug 18, 2010, 12:49:12 AM8/18/10
to
zl2k wrote:
> hi, there,
> I have
> void Foo1(vector<int>& v){
> ....
> A.process(v[i]);
> ....
> }
>
> void Foo2(vector<gsl_vector*>& v){
> ....
> A.process(v[i]);
> ....
> }
>
> How can I integrate Foo1 and Foo2 into a template function? Thanks.
>
> zl2k

Assuming (living dangerously) that I'm not missing a subtlety, what's
wrong with the obvious? :)

template <typename T>
void Foo(vector<T>& v)


{
....
A.process(v[i]);
....
}

Cheers,
Stu

Daniel Krügler

unread,
Aug 18, 2010, 12:48:32 AM8/18/10
to
On 18 Aug., 02:18, zl2k <kdsfin...@gmail.com> wrote:
> hi, there,
> I have
> void Foo1(vector<int>& v){
> ....
> A.process(v[i]);
> ....
>
> }
>
> void Foo2(vector<gsl_vector*>& v){
> ....
> A.process(v[i]);
> ....
>
> }
>
> How can I integrate Foo1 and Foo2 into a template function? Thanks.

Why is

template<class T>
void foo_template(vector<T>& v) {


....
A.process(v[i]);
....
}

insufficient?

[Your question was ambiguous. Please specify your problem
the next time a bit clearer. It also reduces unnecessary
noise in this group]

HTH & Greetings from Bremen,

Daniel Krügler

Louis Lavery

unread,
Aug 18, 2010, 2:35:46 PM8/18/10
to
On 18/08/2010 01:18, zl2k wrote:
> hi, there,
> I have
> void Foo1(vector<int>& v){
> ....
> A.process(v[i]);
> ....
> }
>
> void Foo2(vector<gsl_vector*>& v){
> ....
> A.process(v[i]);
> ....
> }
>
> How can I integrate Foo1 and Foo2 into a template function? Thanks.

Is something like this...

template<typename T> get_t(T& t)
{
return t;
}

template<typename T> get_t(T*& t)
{
return *t;
}

template<typename T>


void foo_template(vector<T>& v)
{

T& get_t(v[i]);

A.process(t);
}

....what you're after (it needs some more work/thought).

Louis.

0 new messages