Template specilization problems, howto work around

G

g18c

Hi, i am having lots of fun with templates :( The following code is
causing me a real headache:

#include <vector>
using namespace std;

class Writer
{
public:
template<typename T> void Write(vector<T>& a)
{
}

template<typename T> void Write(T& t)
{
}

};

int main(int argc, char* argv[])
{
Writer w;
vector<int> v;
w.Write<vector<int> >(v); // calls Write(T& t) with VC7 and VC8
w.Write<int>(v); // calls Write(T& t), OR WITH VC8 Write(vector<T>& a)
is called
return 0;
}

the problem is that since i am using visual studio .net (vc7) i have to
specialise which function to call (VC8 works ok if calling w.Write(v),
it will call Write(vector<T>& a) correctly, however i cant use vc8 here
for development... it would be great to have code which works across
all compilers).

As above, it seems that even if i specify which function to call,
Write(T& t) gets called regardless! Surely specyfying
w.Write<vector<int> >(v); is implying i want the vector version of the
Write function to be called?? I am obviously missing something major
here and im sure this is a lack of understanding of my c++ rather than
a compiler issue (hence the post here!), is there a work around so i
can get the desired result?

TIA, from a very confused Chris!
 
V

Victor Bazarov

Hi, i am having lots of fun with templates :( The following code is
causing me a real headache:

#include <vector>
using namespace std;

class Writer
{
public:
template<typename T> void Write(vector<T>& a)
{
}

template<typename T> void Write(T& t)
{
}

};

int main(int argc, char* argv[])
{
Writer w;
vector<int> v;
w.Write<vector<int> >(v); // calls Write(T& t) with VC7 and VC8
w.Write<int>(v); // calls Write(T& t), OR WITH VC8 Write(vector<T>& a)
is called
return 0;
}

the problem is that since i am using visual studio .net (vc7) i have to
specialise which function to call (VC8 works ok if calling w.Write(v),
it will call Write(vector<T>& a) correctly, however i cant use vc8 here
for development... it would be great to have code which works across
all compilers).

It probably works fine across all compilers. You just mix up VC7 into
"all" compilers when you shouldn't. It has been obsoleted by VC7.1 and
now VC8. If you cannot use VC8 or VC7.1, you need to ask either Microsoft
or in a Microsoft forum (microsoft.public.vc.language) for a work-around.
As above, it seems that even if i specify which function to call,
Write(T& t) gets called regardless!

I just tried it with VC7.1 and it worked fine (just like in VC8).
> Surely specyfying
w.Write<vector<int> >(v); is implying i want the vector version of the
Write function to be called??

Yes. A more specialised template should be chosen.
> I am obviously missing something major
here and im sure this is a lack of understanding of my c++ rather than
a compiler issue (hence the post here!), is there a work around so i
can get the desired result?

Upgrade your compiler.

V
 
G

g18c

Thanks for the reply but the main question i guess is why calling:

w.Write<vector<int> >(v);

actually ends up calling: Write(T& t), rather than the vector version!

I will check this with gcc shortly, but it does this with latest
compiler i have and i presume the same applies to gcc. Will post back
results shortly.
 
V

Victor Bazarov

Thanks for the reply but the main question i guess is why calling:

w.Write<vector<int> >(v);

actually ends up calling: Write(T& t), rather than the vector version!

I will check this with gcc shortly, but it does this with latest
compiler i have and i presume the same applies to gcc. Will post back
results shortly.

Without seeing the original context (learn to quote your own posts!)
I cannot remember what you're referring to. Wasn't it about a bug in
the compiler?

Anyway, perhaps when you can verify the behaviour with a more compliant
compiler (like Comeau online), you could post again asking this other
question. If it hasn't been answered yet, I mean.

V
 

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top