Templates

M

MiniDisc_2k2

Okay I just came up with this scenario and wanted to know if it was
possible. Thus, this is extremely generic code and serves no practical
purpose:

template <class T, class N>
class A
{
// ... some members ...
void f(A<T,>) // is this possible?
};

template <class T, class N>
void A::f(A<T,> data)
{
// coding
}


In case you couldn't find out what I was doing, I was trying to allow the
template to access any type for the second type, like this:

int main()
{
A<int, char> a1;
A<int, float> a2;
a1.f(a2); // this is legal
a1.f(a1); // this is also legal
}

As you can see, the compiler doesn't worry about the type of the second
typename. Is there any way to do this? Or (now that I think about it) can
you make a template of a template?
 
S

Stuart Golodetz

MiniDisc_2k2 said:
Okay I just came up with this scenario and wanted to know if it was
possible. Thus, this is extremely generic code and serves no practical
purpose:

template <class T, class N>
class A
{
// ... some members ...
void f(A<T,>) // is this possible?
};

template <class T, class N>
void A::f(A<T,> data)
{
// coding
}


In case you couldn't find out what I was doing, I was trying to allow the
template to access any type for the second type, like this:

int main()
{
A<int, char> a1;
A<int, float> a2;
a1.f(a2); // this is legal
a1.f(a1); // this is also legal
}

As you can see, the compiler doesn't worry about the type of the second
typename. Is there any way to do this? Or (now that I think about it) can
you make a template of a template?

I'm not completely sure about this, but FWIW:

#include <iostream>
#include <typeinfo>

template <typename T, typename U>
struct X
{
template <typename V>
void f(X<T,V> data);
};

template <typename T, typename U>
template <typename V>
void X<T,U>::f(X<T,V> data)
{
// Just to show the difference:
const std::type_info& t = typeid(data);
std::cout << t.name() << '\n';
}

int main()
{
X<int,char> x1;
X<int,float> x2;
x1.f(x1);
x1.f(x2);
return 0;
}

HTH,

Stuart.

P.S. Don't try and compile this on VC++ 6.0, it won't work.
 
T

tom_usenet

Okay I just came up with this scenario and wanted to know if it was
possible. Thus, this is extremely generic code and serves no practical
purpose:

template <class T, class N>
class A
{
// ... some members ...
void f(A<T,>) // is this possible?

Do you mean:

template <class U>
void f(A<T,U>)
{
}

?
};

template <class T, class N>
void A::f(A<T,> data)
{
// coding
}


In case you couldn't find out what I was doing, I was trying to allow the
template to access any type for the second type, like this:

int main()
{
A<int, char> a1;
A<int, float> a2;
a1.f(a2); // this is legal
a1.f(a1); // this is also legal
}

As you can see, the compiler doesn't worry about the type of the second
typename. Is there any way to do this? Or (now that I think about it) can
you make a template of a template?

You can write member template functions.

Tom
 
S

Stuart Golodetz

Victor Bazarov said:
"MiniDisc_2k2" <[email protected]> wrote...
You probably mean you need a member template...

template <class T, class N> class A
{
public:
template<class U> void f_1(A<T,U>);
template<class U> void f_2(U);
};

int main()
{
A<int,char> aic;
A<int,double> aid;
A<char,int> aci;

aic.f_1<double>(aid); // 'U' cannot be figured out

Why can't U be figured out, out of interest? g++ 3.2 seems to be deducing
it; whether or not it should be is another matter. I replaced the above line
with aic.f_1(aid); and it still worked.

Cheers,

Stuart.
 
T

tom_usenet

I don't know why it can't be figured out (if the question is about
the difficulties implementers face -- I have not written a single
C++ compiler). However, AFAICT, the Standard does not list this
situation as one for figuring out the type.

It's covered by

TT<T>

isn't it? This is a situation where is is perfectly possible to
determine the type, and the standard requires that the compiler deduce
the argument as double in this case.

And there are compilers
that don't figure it out (since the Standard allows not to). As to
why this is not in the Standard, please ask in comp.std.c++.

But it can be figured out. Perhaps you're thinking of deduction of
parents of nested types?

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top