473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Template specilization problems, howto work around

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!

Feb 1 '06 #1
3 1667
g1**@hotmail.com wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 1 '06 #2
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.

Feb 3 '06 #3
g1**@hotmail.com wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ma740988 | last post by:
In an attempt to write generic code for data types int, float, double..., a template seems like the most viable solution. So now, the .h file contains the prototypes for the functions and the...
11
by: Matt | last post by:
I'm posting this question for one of my developers who's not quite as newsgroup-savvy. Any suggestions? The question follows, along with relevant source code. -Matt I have a templated...
4
by: shuisheng | last post by:
Dear All, I want to get a array template like Array<class T, size_t n0, size_t n1, size_t n2 ...> The dimension can be arbitrary. Is it possible? Thanks for your help.
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
4
by: AndrewD | last post by:
Hey C++ folks, I created this today, just for fun. You can make object allocation for any class around 6 times faster, simply by doing the following. class MyClass : public...
9
by: rtalbot | last post by:
I've got a container that looks like this: template <class T> class Foo { public: Foo() : _data(), _status(1) { } Foo(T) : _data(T), _status(0) { } ~Foo() { }
3
by: Bill | last post by:
Hi, I have a quetion how to specialization a non-type templat function, I use VC++ 2005. My code is: #include "stdafx.h" enum VariableType {
8
by: Tim Frink | last post by:
Hi, I want to use a callback function together with templates. Let's say I've this code: File a.h: class A { private:
1
by: farseerfc | last post by:
Hi, everyone here loves C++; As a student studying data structure, I'm writting a single list (with only one pointer to identity the next node in its node struct) simulating std::list like: ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.