passing a type

Hi,

I'm currently using a simple i/o conversion function

template <class T, class U>
inline void ss_convert(const T& t, U& u){
stringstream ss;
ss << t;
ss >> u;
}

I know I'm not checking for any errors, but set that aside for the moment. How can I modify the code to instantiate an object of type U and then return it, without passing a reference? I.e., I'd like to be able to write

T t = T();
U u = ss_convert(t, U)

where the second argument to ss_convert is the class (e.g. int) itself rather than an instance of the class. Is this possible?
I've used a dummy function that I specialise. For example:
1
2
template <typename S, typename D>
inline D convert_cast(S, D* = 0) { return D(); }


Followed by acutal types:
1
2
3
4
5
6
7
8
9
10
11
template <>	inline CString		convert_cast<in_addr, CString>(in_addr, CString*);
template <>	inline CString		convert_cast<ip_address, CString>(ip_address, CString*);
template <>	inline ip_address	convert_cast<CString, ip_address>(CString, ip_address*);
template <>	inline in_addr		convert_cast<CString, in_addr>(CString, in_addr*);
template <>	inline in_addr		convert_cast<ip_address, in_addr>(ip_address, in_addr*);
template <>	inline ip_address	convert_cast<in_addr, ip_address>(in_addr, ip_address*);

template <>	inline timeval	convert_cast<FILETIME, timeval>(FILETIME ft, timeval*);
template <>	inline FILETIME	convert_cast<timeval, FILETIME>(timeval ts, FILETIME*);

//... 
Last edited on
Topic archived. No new replies allowed.