473,583 Members | 3,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Runtime type checking in a simple template function

Dear all,

I have a simple template to convert its argument to a string:

template<typena me T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}

This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1

The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:

+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".

I am not so sure if output manipulators can help me.

Best regards,

Mar 18 '07 #1
11 5656
utab wrote:
Dear all,

I have a simple template to convert its argument to a string:

template<typena me T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}

This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1

The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:

+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".

I am not so sure if output manipulators can help me.
Why bother. Specialise or overload.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 18 '07 #2
Why bother. Specialise or overload.

Hmm, I will read on template specialization. Is that right

Thanks
Mar 18 '07 #3
On 2007-03-18 17:58, utab wrote:
Dear all,

I have a simple template to convert its argument to a string:
By the way, regarding the subject of your post: templates are compile-
time so runtime typechecking won't do you any good since all the
decisions are already made when the app runs.

--
Erik Wikström
Mar 18 '07 #4
On 2007-03-18 17:58, utab wrote:
Dear all,

I have a simple template to convert its argument to a string:

template<typena me T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}

This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1

The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:

+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".

I am not so sure if output manipulators can help me.
Yes, since the bahaviour when the argument is an int is what you want
all you need to do is to fix the behaviour when the value is a double.
This you can do by proividing special double-version of the method:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>

template<typena me T>
std::string AtoStr(T t){
std::ostringstr eam sstrm;
sstrm << t;
return sstrm.str();
}

std::string AtoStr(double t){
std::ostringstr eam sstrm;
int i = t;
if (i == t)
sstrm << std::fixed << std::setprecisi on(1) << t;
else
sstrm << t;
return sstrm.str();
}
int main()
{
std::cout << AtoStr(1.0) << "\n";
std::cout << AtoStr(1.13456) << "\n";
std::cout << AtoStr("sdtfg") << "\n";
}

The if (i == t) business is just to check if the number is an integer
(not the type) in which case we don't need to print more than one 0
after the dot. You might want to work a bit at how you want other
doubles represented (number of digits and so on).

Notice that this does not work for floats. You can use this for floats:

std::string AtoStr(float t)
{
return AtoStr(double(t ));
}

--
Erik Wikström
Mar 18 '07 #5
On Mar 18, 7:27 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-03-18 17:58, utab wrote:
Dear all,
I have a simple template to convert its argument to a string:
template<typena me T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}
This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1
The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:
+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".
I am not so sure if output manipulators can help me.

Yes, since the bahaviour when the argument is an int is what you want
all you need to do is to fix the behaviour when the value is a double.
This you can do by proividing special double-version of the method:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>

template<typena me T>
std::string AtoStr(T t){
std::ostringstr eam sstrm;
sstrm << t;
return sstrm.str();

}

std::string AtoStr(double t){
std::ostringstr eam sstrm;
int i = t;
if (i == t)
sstrm << std::fixed << std::setprecisi on(1) << t;
else
sstrm << t;
return sstrm.str();

}

int main()
{
std::cout << AtoStr(1.0) << "\n";
std::cout << AtoStr(1.13456) << "\n";
std::cout << AtoStr("sdtfg") << "\n";

}

The if (i == t) business is just to check if the number is an integer
(not the type) in which case we don't need to print more than one 0
after the dot. You might want to work a bit at how you want other
doubles represented (number of digits and so on).

Notice that this does not work for floats. You can use this for floats:

std::string AtoStr(float t)
{
return AtoStr(double(t ));

}

--
Erik Wikström
Thanks

Mar 18 '07 #6
"Erik Wikström" <Er***********@ telia.comwrote in message
news:Vv******** ***********@new sb.telia.net...
On 2007-03-18 17:58, utab wrote:
>Dear all,

I have a simple template to convert its argument to a string:

template<typen ame T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}

This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1

The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:

+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".

I am not so sure if output manipulators can help me.

Yes, since the bahaviour when the argument is an int is what you want all
you need to do is to fix the behaviour when the value is a double. This
you can do by proividing special double-version of the method:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>

template<typena me T>
std::string AtoStr(T t){
std::ostringstr eam sstrm;
sstrm << t;
return sstrm.str();
}

std::string AtoStr(double t){
std::ostringstr eam sstrm;
int i = t;
if (i == t)
Wouldn't this be better as:

if ( typeid( i ) == typeid( double ) )
sstrm << std::fixed << std::setprecisi on(1) << t;
else
sstrm << t;
return sstrm.str();
}
int main()
{
std::cout << AtoStr(1.0) << "\n";
std::cout << AtoStr(1.13456) << "\n";
std::cout << AtoStr("sdtfg") << "\n";
}

The if (i == t) business is just to check if the number is an integer (not
the type) in which case we don't need to print more than one 0 after the
dot. You might want to work a bit at how you want other doubles
represented (number of digits and so on).

Notice that this does not work for floats. You can use this for floats:

std::string AtoStr(float t)
{
return AtoStr(double(t ));
}

--
Erik Wikström

Mar 18 '07 #7
On Mar 19, 3:27 am, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-03-18 17:58, utab wrote:
Dear all,
I have a simple template to convert its argument to a string:
template<typena me T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}
This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1
The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:
+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".
I am not so sure if output manipulators can help me.

Yes, since the bahaviour when the argument is an int is what you want
all you need to do is to fix the behaviour when the value is a double.
This you can do by proividing special double-version of the method:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>

template<typena me T>
std::string AtoStr(T t){
std::ostringstr eam sstrm;
sstrm << t;
return sstrm.str();

}

std::string AtoStr(double t){
std::ostringstr eam sstrm;
int i = t;
if (i == t)
sstrm << std::fixed << std::setprecisi on(1) << t;
else
sstrm << t;
return sstrm.str();

}

int main()
{
std::cout << AtoStr(1.0) << "\n";
std::cout << AtoStr(1.13456) << "\n";
std::cout << AtoStr("sdtfg") << "\n";

}

The if (i == t) business is just to check if the number is an integer
(not the type) in which case we don't need to print more than one 0
after the dot. You might want to work a bit at how you want other
doubles represented (number of digits and so on).

Notice that this does not work for floats. You can use this for floats:

std::string AtoStr(float t)
{
return AtoStr(double(t ));

}

--
Erik Wikström
Seems this is the right method.
You can make use of iomanip function to control the input and output.

Regards,
Sarath

http://sarathc.wordpress.com/

Mar 18 '07 #8
On Mar 19, 3:27 am, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-03-18 17:58, utab wrote:
Dear all,
I have a simple template to convert its argument to a string:
template<typena me T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}
This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1
The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:
+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".
I am not so sure if output manipulators can help me.

Yes, since the bahaviour when the argument is an int is what you want
all you need to do is to fix the behaviour when the value is a double.
This you can do by proividing special double-version of the method:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>

template<typena me T>
std::string AtoStr(T t){
std::ostringstr eam sstrm;
sstrm << t;
return sstrm.str();

}

std::string AtoStr(double t){
std::ostringstr eam sstrm;
int i = t;
if (i == t)
sstrm << std::fixed << std::setprecisi on(1) << t;
else
sstrm << t;
return sstrm.str();

}

int main()
{
std::cout << AtoStr(1.0) << "\n";
std::cout << AtoStr(1.13456) << "\n";
std::cout << AtoStr("sdtfg") << "\n";

}

The if (i == t) business is just to check if the number is an integer
(not the type) in which case we don't need to print more than one 0
after the dot. You might want to work a bit at how you want other
doubles represented (number of digits and so on).

Notice that this does not work for floats. You can use this for floats:

std::string AtoStr(float t)
{
return AtoStr(double(t ));

}

--
Erik Wikström
IMO, What Erik Said is the best solution.
You can make use of iomanip function to control the input and output.

Regards,
Sarath

http://sarathc.wordpress.com/

Mar 19 '07 #9
On 19 Mar, 00:19, "Jim Langston" <tazmas...@rock etmail.comwrote :
"Erik Wikström" <Erik-wikst...@telia. comwrote in message
std::string AtoStr(double t){
std::ostringstr eam sstrm;
int i = t;
if (i == t)

Wouldn't this be better as:

if ( typeid( i ) == typeid( double ) )
No, the above will always be false since i is an int. The purpose if
the above is to check if the value of t is an integer (not of type
integer) in which case we only want to add a decimal sign and one
zero. If it isn't we will use more precision.

--
Erik Wikström

Mar 19 '07 #10

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

Similar topics

3
2346
by: George Sakkis | last post by:
Explicit type checking is not typically seen in Python code, and usually that's not a big problem; most typing errors are likely to raise a TypeError or AttributeError sooner than later. There are cases, though, where typing errors are not caught (at least not early) because different classes happen to have methods with the same name; that's...
6
14309
by: Web Developer | last post by:
Hi, I come across the term "type checking" very often in my readings on C++, and have never heard it in Java. Besides the simplistic answer that it checks the "type", what more does it mean? WD
1
2718
by: Patrick Kowalzick | last post by:
Hello all, I want to test run-time if a template function for a special type exists. See example below. The checkfoo function is where I need some hints. Regards, Patrick #include <iostream>
5
2430
by: Big Blue Ox | last post by:
Hello, I am trying to store different types that are all derived from the same base class in a list template. So for instance lets say I have this is fragment of skeleton code: class Pet { string name;
2
2308
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just want to know how it works with the overloading of get<>(). boost::tupple<int,doubletup(1,2.0); double d=tup.get<2>(); // equal 2.0 // simple...
4
2283
by: infogoogle | last post by:
Hello, i'm having problems with the type of a template function: This code: class A {}; class B : A {}; template<class T B* fnull() { return 0; };
7
9435
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void f() const; };
5
2357
by: chrisstankevitz | last post by:
Hi, Q1: Is there a way to make a template function that works only for specific types which produces a compiler error if used with an invalid type? Q2: If not, how do people deal with this issue? I believe A1 is no. I tried below with template function "f". For A2, functors work but have ugly syntax and runtime overhead. A
10
4522
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an integer, it should return false, but it should work normally if the parameter is a float. To illustrate this, I attached a minimal example.
0
7895
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8182
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8327
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7935
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5374
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3818
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3843
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1433
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.