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

template specialization for unsigned types

2
Hello all,

First, some mostly gratuitous background, which you can safely skip if you're not interested, or feel free to comment/give suggestions:
I am writing a configuration interface for a DAQ system which reads in text-based configuration files. Because of the large number of parameters which might need to be set and their grouping into a nice tree-like structure, I've created a ParameterNode class, which holds a map<string,ParameterNode*> of all that node's children (or branches, leaves, whatever). Then to store simple variables, there is a templated Parameter<class T> class which inherits from ParameterNode. This allows me to have arbitrarily complex parameter structures without having to re-write any of the iostream interface.

So here's the problem. My Parameter class looks like
Expand|Select|Wrap|Line Numbers
  1. template<class T> Parameter{
  2. public:
  3.      Parameter(const T& val) : _val(T) {}
  4.      ~Parameter() {}
  5.      std::istream& ReadFrom(std::istream& in) { return in>>_val; }
  6.      std::ostream& WriteTo(std::ostream& out) { return out<<_val; }
  7. private:
  8.      T _val;
  9. };
  10.  
Now, I'd like to have any unsigned values be written/read with the form 0x1EF8, etc, i.e. turn on hex and showbase. So for instance I'd get something like

Expand|Select|Wrap|Line Numbers
  1. template<> inline std::istream& Parameter<unsigned>::ReadFrom(std::istream& in)
  2. {
  3.   in>>std::hex>>std::showbase>>_val>>std::noshowbase>>std::dec;
  4. }
So, will this work for any unsigned type, or do I have to declare separate instantiations for all of unsigned, unsigned char, unsigned short, unsigned long, uintX_t, etc?

Many thanks,
~Ben
Nov 3 '08 #1
2 2094
arnaudk
424 256MB
"unsigned" is a synonym for "unsigned int" so yes you'll have so specify the different specializations explicitly if you want different behavior from signed types, or just don't specialize if it's all the same anyway. You might want to take a look at boost traits which you can use to determine if a type is unsigned.
Nov 4 '08 #2
bloer
2
Thanks! Is there anything Boost can't do? Unfortunately I'm trying to minimize dependencies, so I don't want to include Boost. I noticed that the type_traits headers are also in the tr1 package which should be on most systems, but it doesn't seem to want to compile (well actually it's only linking that's the problem) on my system.

Oh well, I guess specifying them all by hand is is not so bad.
Nov 4 '08 #3

Sign in to post your reply or Sign up for a free account.

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() {}...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
1
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following:...
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...
3
by: Scott Frazer | last post by:
I'm trying to do some template specialization and can't get it to work quite right. I have a templated base class: template <typename T> class SignalBase { ... }; and a derived class: ...
2
by: Yang Zhang | last post by:
I have a small program like this: //////////////////////////////////////////////// #include <iostream> using namespace std ; // set bits in an address template <typename T> inline T*...
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,...
7
by: woessner | last post by:
Hi all, I whipped up a quick class to represent a matrix for use with LAPACK. It's a template class so it can support the 4 data types supported by LAPACK (single/double x complex/real). I...
4
by: Julien Jorge | last post by:
Hi, I have a compilation-time error on the specialization of a method from a imbricated template class, and I can't figure why. The problematic part of the code is resumed below. The error occurs...
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.