473,387 Members | 1,517 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,387 software developers and data experts.

simple(?) question about template

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

Jul 23 '05 #1
13 1215
rod
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
}

Jul 23 '05 #2
dgront wrote:
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...?


How do you plan on using it?

V
Jul 23 '05 #3
rod wrote:
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?
Jul 23 '05 #4
rod
Sorry I was working the other way around. Try this:

template< template<typename U> class T >
T<typename> converter(const string & sData)
{
T<typename> nVar;
nVar.fromString(sData);
return nVar;
}

Jul 23 '05 #5
rod
Sorry I was converting the other way around. Try this:

template< template<typename U> class T >
T<typename> converter(const string & sData)
{
T<typename> nVar;
nVar.fromString(sData);
return nVar;
}

Jul 23 '05 #6
rod wrote:
Sorry I was working the other way around. Try this:

template< template<typename U> class T >
T<typename> converter(const string & sData)
{
T<typename> nVar;
nVar.fromString(sData);
return nVar;
}


Uh... Doesn't seem to work. Here's a complete program, please make
it compile with Comeau online:

#include <string>
#include <sstream>

template<class T> class Point {
public:
T x,y,z;
void fromString(std::string const& s) {
std::istringstream is(s);
is >> x >> y >> z;
}
};

template< template<typename U> class T >
T<typename> converter(const std::string & sData)
{
T<typename> nVar;
nVar.fromString(sData);
return nVar;
}

int main()
{
Point<double> pd;
pd = converter<Point>("1.0 2.0 3.0"); // I am guessing here ???

return 0;
}
Jul 23 '05 #7
In article <11**********************@f14g2000cwb.googlegroups .com>,
"dgront" <dg****@chem.uw.edu.pl> wrote:
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...?

I suggest you use boost's lexical_cast.

Point p = lexical_cast<Point>( myStringRep );

Below is a rough version, the boost version is much more robust.

template < typename T, typename U >
T lexical_cast( const U& arg ) {
std::stringstream ss;
T result;

if ( !( ss << arg && ss >> result ) )
throw std::exception();
return result;
}
Jul 23 '05 #8
rod
That code worked fine for me on VC7.1 but did not on Comeau. The only
way I've got this to work is as follows (which is not necessarily the
way you wanted to use it):

#include <string>
#include <sstream>

template<class T>
class Point
{
public:
T x,y,z;

void fromString(const std::string & s)
{
std::istringstream is(s);
is >> x >> y >> z;
}

void outputString()
{
std::cout << x << " " << y << " " << z << std::endl;
}
};

template< class T >
T converter(const std::string & sData)
{
T nVar;
nVar.fromString(sData);
return nVar;
}

int main()
{
Point<double> pd;
pd = converter< Point<double> >("1.0 2.0 3.0"); // I am guessing here
???
pd.outputString();
return 0;
}

Jul 23 '05 #9
rod wrote:
That code worked fine for me on VC7.1 but did not on Comeau. The only
way I've got this to work is as follows (which is not necessarily the
way you wanted to use it):

[...]
template< class T >
T converter(const std::string & sData)
{
T nVar;
nVar.fromString(sData);
Sure. But going back to the OP's question, how do you make it work with
built-in types now? They don't have 'fromString' member functions, do
they?
return nVar;
}

V
Jul 23 '05 #10
Daniel T. wrote:
In article <11**********************@f14g2000cwb.googlegroups .com>,
"dgront" <dg****@chem.uw.edu.pl> wrote:

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...?
I suggest you use boost's lexical_cast.

Point p = lexical_cast<Point>( myStringRep );


'Point' is not a class. It's a template.

Below is a rough version, the boost version is much more robust.

template < typename T, typename U >
T lexical_cast( const U& arg ) {
std::stringstream ss;
T result;

if ( !( ss << arg && ss >> result ) )
throw std::exception();
return result;
}


V
Jul 23 '05 #11
rod
One way would be to use template specialization...one for each built-in
type you want to convert a string to:

template<>
double converter(const std::string & sData)
{
// Code that converts a string to a double
}

.....

Jul 23 '05 #12
rod
One way would be to use template specialization for each built-in type
you wanted to convert to from a string, i.e:

template<>
double converter(const std::string & sData)
{
// Code that converts to a double
}

Jul 23 '05 #13
Victor Bazarov <v.********@comAcast.net> wrote:
Daniel T. wrote:
In article <11**********************@f14g2000cwb.googlegroups .com>,
"dgront" <dg****@chem.uw.edu.pl> wrote:

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...?


I suggest you use boost's lexical_cast.

Point p = lexical_cast<Point>( myStringRep );


'Point' is not a class. It's a template.


Fine:

Point<int> p = lexical_cast< Point<int> >( myStringRep );

Below is a rough version, the boost version is much more robust.

template < typename T, typename U >
T lexical_cast( const U& arg ) {
std::stringstream ss;
T result;

if ( !( ss << arg && ss >> result ) )
throw std::exception();
return result;
}


V

Jul 23 '05 #14

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

Similar topics

1
by: BekTek | last post by:
I'm still confused about the template partial specialization which is used in many libraries.. due to lack of introduction for beginner.. Could you tell me about that in short? Thanks in...
5
by: wongjoekmeu | last post by:
Hello all, I have a question about templates. Let say I have the following template function ----- template <class T> T max(T a, T b) { return a > b ? a : b ; }
0
by: Daniel Perron | last post by:
Hi, I was trying to use Galois.Net to specify code generators and I had to look for some text template tool to simplify the syntax. I was looking for something similar to the syntax of an...
0
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
5
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to...
3
by: goodmen | last post by:
I think the boost::concept_check is too complex. Here is my solution about static interface and concept. The static interface can be used as function param. It is just a proxy of the real...
17
by: allen.fowler | last post by:
Hello, Can anyone recommend a simple python template engine for generating HTML that relies only on the Pyhon Core modules? No need for caching, template compilation, etc. Speed is not a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.