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, 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
Why bother. Specialise or overload.
Hmm, I will read on template specialization. Is that right
Thanks
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
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
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
"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
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/
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/
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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>
|
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;
|
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...
| |
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; };
|
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;
};
|
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
|
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.
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |