S
sagi.perel
I have tried to compile the following code on Win & Unix. Doesn't work
on either.
<----- CODE ----->
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <typeinfo>
using namespace std;
template <class T>
class MyArray
{
private:
vector<T> _data;
public:
void foo(void* mem_buf, int buf_size)
{
if(typeid(T) == typeid(string))
{
char buf[1000]={0};
strncpy(buf, (char*)mem_buf, buf_size);
_data[0] = string(buf);
return;
}
if(typeid(T) == typeid(int))
{
int tmp;
mempcy((void*)&tmp, mem_buf, sizeof(T));
_data[0] = tmp;
return;
}
}
};
int main()
{
MyArray<int> a;
int b =5;
a.foo((void*)&b, sizeof(b));
}
<---- END CODE --->
Win VC8 gives 2 errors:
1) For the line: _data[0] = string(buf); :
error C2440: '=' : cannot convert from
'std::basic_string<_Elem,_Traits,_Ax>' to 'int'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called
2) For the line: mempcy((void*)&tmp, mem_buf, sizeof(T));:
error C3861: 'mempcy': identifier not found
Unix g++ 4.1.0 20060304 (Red Hat 4.1.0-3) on i386-redhat-linux gives:
test3.cpp: In member function void MyArray<T>::foo(void*, int):
test3.cpp:28: error: there are no arguments to ×’mempcy that depend on
a template parameter, so a declaration of mempcy must be available
test3.cpp:28: error: (if you use -fpermissive, G++ will accept your
code, but allowing the use of an undeclared name is deprecated)
The problem seems to be that the VC8 compiler is trying to compile the
code with the given T=int, so on the line that says _data[0] =
string(buf);
it is trying to insert a string into an <int> vector. I could dig
that, but it seems annoying that there is no way around that. What I
want to achieve is a template that I can use, where I can parse data
that I get according to its type in runtime.
If there is no other option, I will probably use a base class and
derive from it instead of templates. They just seemed a cleaner
solution. If you have any thoughts- I would love to hear them.
Now, g++ doesn't seem to be bothered by this problem- which is kinda
weird.
The other thing is the problem with memcpy. Anyone can shed a light on
that?
Thanks
Sagi
on either.
<----- CODE ----->
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <typeinfo>
using namespace std;
template <class T>
class MyArray
{
private:
vector<T> _data;
public:
void foo(void* mem_buf, int buf_size)
{
if(typeid(T) == typeid(string))
{
char buf[1000]={0};
strncpy(buf, (char*)mem_buf, buf_size);
_data[0] = string(buf);
return;
}
if(typeid(T) == typeid(int))
{
int tmp;
mempcy((void*)&tmp, mem_buf, sizeof(T));
_data[0] = tmp;
return;
}
}
};
int main()
{
MyArray<int> a;
int b =5;
a.foo((void*)&b, sizeof(b));
}
<---- END CODE --->
Win VC8 gives 2 errors:
1) For the line: _data[0] = string(buf); :
error C2440: '=' : cannot convert from
'std::basic_string<_Elem,_Traits,_Ax>' to 'int'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called
2) For the line: mempcy((void*)&tmp, mem_buf, sizeof(T));:
error C3861: 'mempcy': identifier not found
Unix g++ 4.1.0 20060304 (Red Hat 4.1.0-3) on i386-redhat-linux gives:
test3.cpp: In member function void MyArray<T>::foo(void*, int):
test3.cpp:28: error: there are no arguments to ×’mempcy that depend on
a template parameter, so a declaration of mempcy must be available
test3.cpp:28: error: (if you use -fpermissive, G++ will accept your
code, but allowing the use of an undeclared name is deprecated)
The problem seems to be that the VC8 compiler is trying to compile the
code with the given T=int, so on the line that says _data[0] =
string(buf);
it is trying to insert a string into an <int> vector. I could dig
that, but it seems annoying that there is no way around that. What I
want to achieve is a template that I can use, where I can parse data
that I get according to its type in runtime.
If there is no other option, I will probably use a base class and
derive from it instead of templates. They just seemed a cleaner
solution. If you have any thoughts- I would love to hear them.
Now, g++ doesn't seem to be bothered by this problem- which is kinda
weird.
The other thing is the problem with memcpy. Anyone can shed a light on
that?
Thanks
Sagi