473,403 Members | 2,284 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,403 software developers and data experts.

A Template that rejects classes.

I've got a bit of code that shows which bits of a variable are 1 or 0
(OK, I'm making assumptions about 8 bit bytes and significance.)

But what I want to do is only allow this template to be instantiated
for base types, not classes.
I am averse to RTTI for this, but I keep thinking there's a bit of
syntactic sugar that will do it cleanly.
Solutions of the form "classes are bigger than 64 bits so reject them"
don't count :)

template <typename Tstring DumpBits (T x)
{
unsigned int mask=1;
int i;
string s;

for ( i= 0; i != sizeof(x)*8; i++, mask *=2)
{
mask & x ? s = "1" + s : s = "0" + s;
}
return s;
}

May 9 '07 #1
6 1356
Dominic Connor, Pimp wrote:
I've got a bit of code that shows which bits of a variable are 1 or 0
(OK, I'm making assumptions about 8 bit bytes and significance.)

But what I want to do is only allow this template to be instantiated
for base types, not classes.
I am averse to RTTI for this, but I keep thinking there's a bit of
syntactic sugar that will do it cleanly.
Solutions of the form "classes are bigger than 64 bits so reject them"
don't count :)

template <typename Tstring DumpBits (T x)
{
unsigned int mask=1;
int i;
string s;

for ( i= 0; i != sizeof(x)*8; i++, mask *=2)
{
mask & x ? s = "1" + s : s = "0" + s;
}
return s;
}
Find a solution to recognize something as a class (SFINAE is used
along with "a pointer to member" syntax), and invert it. Look on the
web for "type is a class" template solution.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 9 '07 #2
Dominic Connor, Pimp wrote:
I've got a bit of code that shows which bits of a variable are 1 or 0
(OK, I'm making assumptions about 8 bit bytes and significance.)

But what I want to do is only allow this template to be instantiated
for base types, not classes.
I am averse to RTTI for this, but I keep thinking there's a bit of
syntactic sugar that will do it cleanly.
Solutions of the form "classes are bigger than 64 bits so reject them"
don't count :)

template <typename Tstring DumpBits (T x)
{
unsigned int mask=1;
int i;
string s;

for ( i= 0; i != sizeof(x)*8; i++, mask *=2)
{
mask & x ? s = "1" + s : s = "0" + s;
}
return s;
}
That code will not work for 64 bit types on many platforms.

You can list all the types you want it to work for by using specializations.

May 9 '07 #3
"Gianni Mariani" <gi*******@mariani.wswrote in message
news:46**********************@per-qv1-newsreader-01.iinet.net.au...
Dominic Connor, Pimp wrote:
>I've got a bit of code that shows which bits of a variable are 1 or 0
(OK, I'm making assumptions about 8 bit bytes and significance.)

But what I want to do is only allow this template to be instantiated
for base types, not classes.
I am averse to RTTI for this, but I keep thinking there's a bit of
syntactic sugar that will do it cleanly.
Solutions of the form "classes are bigger than 64 bits so reject them"
don't count :)

template <typename Tstring DumpBits (T x)
{
unsigned int mask=1;
Are you sure you don't really want
T mask = 1?
although I don't think that would work with enums, but what about when T is
larger than the size of unsigned int?
>int i;
string s;

for ( i= 0; i != sizeof(x)*8; i++, mask *=2)
{
mask & x ? s = "1" + s : s = "0" + s;
Not that it really matters, but would probably code this line like this:

s = (mask & x ? "1" : "0") + s;

Although I've had problems before trying to contcatenate to a string when
the first element is a char pointer. I guess operator= handles that.
>}
return s;
}

That code will not work for 64 bit types on many platforms.

You can list all the types you want it to work for by using
specializations.
Why would this not work on some 64 bit implemenations? What is the
limitation that would prevent it from working? Is it the unsigned int?
May 10 '07 #4
On May 9, 11:01 am, "Dominic Connor, Pimp" <dominic.con...@gmail.com>
wrote:
I've got a bit of code that shows which bits of a variable are 1 or 0
(OK, I'm making assumptions about 8 bit bytes and significance.)

But what I want to do is only allow this template to be instantiated
for base types, not classes.
I am averse to RTTI for this, but I keep thinking there's a bit of
syntactic sugar that will do it cleanly.
Solutions of the form "classes are bigger than 64 bits so reject them"
don't count :)

template <typename Tstring DumpBits (T x)
{
unsigned int mask=1;
int i;
string s;

for ( i= 0; i != sizeof(x)*8; i++, mask *=2)
{
mask & x ? s = "1" + s : s = "0" + s;
}
return s;

}
I would recommend using the TR1 (or boost) type_traits class
templates; in particular, std::tr1::is_integral<or
std::tr1::is_fundamental<could be used to screen for suitable
template type arguments.

Greg

May 10 '07 #5
On May 9, 11:01 am, "Dominic Connor, Pimp" <dominic.con...@gmail.com>
wrote:
I've got a bit of code that shows which bits of a variable are 1 or 0
(OK, I'm making assumptions about 8 bit bytes and significance.)

But what I want to do is only allow this template to be instantiated
for base types, not classes.
Make the template function private to your module. Instead of exposing
the template function, expose a set of overloaded functions which use
the private function for their implementation.

template <typename Tvoid dump_bits_impl(T x) { /* ... */ }

int dump_bits(char x) { dump_bits_impl(x); }
int dump_bits(int x) { dump_bits_impl(x); }
int dump_bits(long x) { dump_bits_impl(x); }

// .. etc: repeat for all supported basic types

In spite of the trivial repetition, you're still exploiting the
template as a considerable labor-saving device.
I am averse to RTTI for this, but I keep thinking there's a bit of
syntactic sugar that will do it cleanly.
Solutions of the form "classes are bigger than 64 bits so reject them"
don't count :)
They aren't. How big is class empty {}; on your compiler? :)

May 10 '07 #6
On May 9, 8:01 pm, "Dominic Connor, Pimp" <dominic.con...@gmail.com>
wrote:
I've got a bit of code that shows which bits of a variable are 1 or 0
(OK, I'm making assumptions about 8 bit bytes and significance.)
But what I want to do is only allow this template to be instantiated
for base types, not classes.
I am averse to RTTI for this, but I keep thinking there's a bit of
syntactic sugar that will do it cleanly.
Solutions of the form "classes are bigger than 64 bits so reject them"
don't count :)
template <typename Tstring DumpBits (T x)
{
unsigned int mask=1;
int i;
string s;

for ( i= 0; i != sizeof(x)*8; i++, mask *=2)
{
mask & x ? s = "1" + s : s = "0" + s;
}
return s;

}
For starters, why not write the function something like:

template< typename T >
std::string
dumpBits( T value )
{
std::string result ;
while ( result.size() < sizeof( T ) * CHAR_BIT ) {
result += (value & 1 == 0) ? '0' : '1' ;
// or: result += (value & 1) + '0' ;
value >>= 1 ;
}
std::reverse( result.begin(), result.end() ) ;
return result ;
}

This will reject anything for which & or >isn't defined, which
means anything that isn't either an integral type or designed to
behave like an integral type. Most likely, that's sufficient.
(Of course, the error message isn't very pretty. But then, is
it ever, when you use templates?)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 10 '07 #7

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

Similar topics

21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
3
by: Gianni Mariani | last post by:
I was a little surprised by this: It seems like the code below should not compile but the Comeau 4.3.3 compiler accepts it and the gcc 3.4(prerel) compiler rejects it and MSVC++7.1 ICE's. ...
6
by: Mike Alexeev | last post by:
What is the correct syntax for default values to member function templates? Here is my example: 1 struct A 2 { 3 typedef int typeA; 4 }; 5 6 struct...
1
by: J.T. Conklin | last post by:
I've been working on the configuration infrastructure for the ACE framework which contains code that uses a templatized handle/body idiom with placement new/delete. With g++ 3.4, we found that the...
6
by: Alex | last post by:
I've a problem with following simple code (not really usefull): //--------------------------------------------------------------------------- #pragma hdrstop #include "stdio.h" #include...
11
by: Johan | last post by:
Hi Can somebody explain to me why I get this warning message and how I can solve this warning message. Thanks a lot Johan In member function `void
3
by: Ken Cecka | last post by:
This is a contrived example to demonstrate a syntax problem I'm struggling with: #include <vector> template <typename T> class Class { };
1
by: Kai-Uwe Bux | last post by:
Hi folks, I would like to know which clause of the standard rules out this: template < typename eval > struct recursive_template { typedef typename eval::enum_type Enum;
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
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: 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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
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...

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.