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

Help with template code, and a question on numeric_limits

Hello. As practice in programming using templates, and for interest's
sake, I wrote some code to see how floating point numbers are stored
on my system. I hoped to be able to use it like:

std::cout << internal_rep(1.0F) << std::endl;
std::cout << internal_rep(1.0) << std::endl;

and it would give me a hexadecimal output of the bytes making up the
storage of 1.0 in float and double format.

The code I have written to do this is as follows:

#include <iostream>
#include <iomanip>
#include <vector>
#include <memory>

template<typename T>
class internal_rep_class;

template<typename T>
std::ostream& operator<<(std::ostream& os,
const std::auto_ptr<internal_rep_class<T> >& rep) {

for (unsigned i = 0; i < rep->bytes.size(); ++i)
os << std::hex
<< static_cast<unsigned int>(rep->bytes[i]) << ' ';
return os;
}

template<typename T>
class internal_rep_class {
public:
friend std::ostream& operator<< <>(std::ostream&,
const std::auto_ptr<internal_rep_class<T> >& rep);

internal_rep_class(T t) {
for (unsigned i = 0; i < sizeof(T); ++i)
bytes.push_back(
*(reinterpret_cast<unsigned char*>(&t)+i));
}
private:
std::vector<unsigned char> bytes;
};

template<typename T>
std::auto_ptr<internal_rep_class<T> > internal_rep(T t) {
return std::auto_ptr<internal_rep_class<T> >(
new internal_rep_class<T>(t));
}

int main() {

std::cout << internal_rep(1.0) << std::endl;

return 0;

}

The code apparently does what I want, but it seems overly complicated
to me. In particular the use of std::auto_ptr seems like overkill.

The reason for writing the internal_rep function was to allow me to
write

std::cout << internal_rep(1.0) << std::endl;

rather than the slightly more inconvenient

std::cout << internal_rep_class<double>(1.0) << std::endl;

Initially I wanted internal_rep to return a const reference. However
my understanding is that this would not work: for even though it is
legal to bind a const reference to a temporary, this reference would
be destroyed at the completion of internal_rep, and a copy of it would
actually be returned. Is this correct? (My compiler issued a warning
over the code.)

So I wonder, is there a better way that avoids the use of
std::auto_ptr or similar?

On a slightly related note, I notice that on my system

std::numeric_limits<double>::has_quiet_NaN
and
std::numeric_limits<double>::has_signaling_NaN

are both false, and

std::numeric_limits<double>::quiet_NaN()
and
std::numeric_limits<double>::signaling_NaN()

return 0.0. However

std::cout << (0.0 / 0.0);

outputs NaN. This seems a bit counterintuitive - is it unusual?
Jul 19 '05 #1
0 1507

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

Similar topics

4
by: Marc Schellens | last post by:
I posted a similar question some time ago, but didn't get an satisfying answer. Lets say I have a template and five integer and two floating types. template <typename T> class A { A() {}...
26
by: Alexander Block | last post by:
Hello newsgroup, let's say I have a function like template<class Type> inline bool areEqual(const Type &a, const Type &b) { return ( a == b ); }
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
5
by: Niklas Norrthon | last post by:
I've been banging my head in the wall for some time now over a little problem having to do with partial specialization of function templates. The real problem is more complex than this, but...
12
by: Jim Langston | last post by:
I have a template I call StrmConvert, which uses std::stringstream to convert from any type to any other type that can be used by stringstring. This is what it looks like: template<typename T,...
11
by: ALiX | last post by:
Hi! My template class has a const static data member, which is used by the constructor. It seems that the program does not produce the correct result. Either it is the fault of the compiler (gcc...
1
by: Knut Stolze | last post by:
Hi, Let's assume I have a namespace that contains a class definition. Now I would like to add a template specialization for std::numeric_limits for the new class. I want to have this...
10
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...

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.