关于模板的简单(?)问题 [英] simple(?) question about template
问题描述
也许我的问题太简单了,但我花了一些时间在它上面
仍然不知道..
我需要一个模板函数,从字符串转换为其他数据类型:
template< typename T>转换器(std :: string s);
对于T = double,int等,它工作正常。但是如何为一个已经是模板的类型
做这件事,比如
模板< typename T> class Point {T x,y,z; };
模板< typename T> class AnotherClass {T data; };
如何让转换器同时适用于Point< T>,AnotherClacc< T>,
double,int等...?
多米尼克
Maybe my question is too simple, but I''ve spent some time on it and
still don''t know..
I need a template function, converting from string to other data types:
template <typename T> converter(std::string s);
For T = double, int, etc. it works fine. But how to do it for a type
which already is a template, like
template<typename T> class Point { T x, y,z; };
template<typename T> class AnotherClass { T data; };
How to make converter working both for Point<T>, AnotherClacc<T>,
double, int, etc...?
Dominik
推荐答案
我这样做的方法如下:
模板<模板< typename U> class T>
void converter(string& sName)
{
T< typename> nVar;
sName = nVar.toString(); \\或其他任何字符串转换函数
}
A way I got this to work is as follows:
template< template<typename U> class T >
void converter(string & sName)
{
T<typename> nVar;
sName = nVar.toString(); \\ or whatever string conversion function
}
dgront写道:
也许是我的问题太简单了,但我已经花了一些时间在它上面而且还不知道..
我需要一个模板函数,从字符串转换为其他数据类型:
模板< typename T> converter(std :: string s);
你的意思是,
模板< typename T> T转换器(std :: string const& s);
,不是吗?
对于T = double,int等,它有效精细。但是如何为一个已经是模板的类型做这件事,
没有这样的事情。这是一种类型或模板。
喜欢模板< typename T> class Point {T x,y,z;模板< typename T> class AnotherClass {T data;如何使转换器同时适用于Point< T>,AnotherClacc< T>,
double,int等...?
Maybe my question is too simple, but I''ve spent some time on it and
still don''t know..
I need a template function, converting from string to other data types:
template <typename T> converter(std::string s);
You mean,
template<typename T> T converter(std::string const& s);
, don''t you?
For T = double, int, etc. it works fine. But how to do it for a type
which already is a template,
There is no such thing. It''s either a type or a template.
like
template<typename T> class Point { T x, y,z; };
template<typename T> class AnotherClass { T data; };
How to make converter working both for Point<T>, AnotherClacc<T>,
double, int, etc...?
你打算如何使用它?
V
How do you plan on using it?
V
rod写道:
我使用它的方法如下:
template<模板< typename U> class T>
void converter(string& sName)
''void''?
{
T< typename> ; nVar;
sName = nVar.toString(); \\或其他任何字符串转换函数
}
A way I got this to work is as follows:
template< template<typename U> class T >
void converter(string & sName)
''void''?
{
T<typename> nVar;
sName = nVar.toString(); \\ or whatever string conversion function
}
那么,价值如何回归?你怎么看它用的?
So, how does the value get back? And how do you see it used?
这篇关于关于模板的简单(?)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!