Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A Template that rejects classes.

Reply
Thread Tools

A Template that rejects classes.

 
 
Dominic Connor, Pimp
Guest
Posts: n/a
 
      05-09-2007
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 T> string 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;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-09-2007
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 T> string 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


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      05-09-2007
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 T> string 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.

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      05-10-2007
"Gianni Mariani" <(E-Mail Removed)> wrote in message
news:46423545$0$9105$(E-Mail Removed)...
> 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 T> string 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?


 
Reply With Quote
 
Greg Herlihy
Guest
Posts: n/a
 
      05-10-2007
On May 9, 11:01 am, "Dominic Connor, Pimp" <(E-Mail Removed)>
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 T> string 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

 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      05-10-2007
On May 9, 11:01 am, "Dominic Connor, Pimp" <(E-Mail Removed)>
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 T> void 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?

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-10-2007
On May 9, 8:01 pm, "Dominic Connor, Pimp" <(E-Mail Removed)>
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 T> string 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:(E-Mail Removed)
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Server rejects template script request helveticus ASP .Net 1 07-02-2009 07:10 AM
Blogger template rejects javascript loop Jim Javascript 7 11-15-2007 10:10 AM
Sonic Record Now Plus DVD Burner Rejects Certain Files: Why ? Robert11 Computer Support 3 10-06-2005 04:20 PM
SMTP server rejects sender's email address Klaatu Computer Support 13 05-22-2004 07:47 PM



Advertisments