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!
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!