Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default template argument in a function: reasonable but disallowed

Reply
Thread Tools

Default template argument in a function: reasonable but disallowed

 
 
chrisstankevitz@yahoo.com
Guest
Posts: n/a
 
      11-09-2006
Apparently default template arguments can only be used in classes. I
include two apps below. One works but is IMO messy. The other does
not work and is IMO clean.

Q1: Why would c++ dissallow the clean version?
Q2: Is there a way to do what I want to do "cleanly" (as I define it

The templated function writes a long to a binary stream in 64 bits.
This code works for 32 and 64 bit long.

Thanks!

Chris

//===== This code is messy, allows for user error, but compiles =====

#include <fstream>

template<typename Type, int NumBits>
void BinaryWrite64Bit(Type Value, std:stream& Stream);

template<>
void BinaryWrite64Bit<long, 32>(long Value, std:stream& Stream)
{
static const long Zero = 0;
Stream.write(reinterpret_cast<const char*>(&Zero), sizeof(Zero));
Stream.write(reinterpret_cast<const char*>(&Value), sizeof(Value));
}

template<>
void BinaryWrite64Bit<long, 64>(long Value, std:stream& Stream)
{
Stream.write(reinterpret_cast<const char*>(&Value), sizeof(Value));
}

int main()
{
std:fstream Stream;

long Value = 10;

BinaryWrite64Bit<long, sizeof(long)*8>(Value, Stream);

return 0;
}

//===== This code is clean, robust but doesn't compile =====
#include <fstream>

template<typename Type, int NumBits = sizeof(Type)*8>
void BinaryWrite64Bit(Type Value, std:stream& Stream);

template<>
void BinaryWrite64Bit<long, 32>(long Value, std:stream& Stream)
{
static const long Zero = 0;
Stream.write(reinterpret_cast<const char*>(&Zero), sizeof(Zero));
Stream.write(reinterpret_cast<const char*>(&Value), sizeof(Value));
}

template<>
void BinaryWrite64Bit<long, 64>(long Value, std:stream& Stream)
{
Stream.write(reinterpret_cast<const char*>(&Value), sizeof(Value));
}

int main()
{
std:fstream Stream;

long Value = 10;

BinaryWrite64Bit(Value, Stream);

return 0;
}

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      11-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Apparently default template arguments can only be used in classes. I
> include two apps below. One works but is IMO messy. The other does
> not work and is IMO clean.
>
> Q1: Why would c++ dissallow the clean version?
> Q2: Is there a way to do what I want to do "cleanly" (as I define it
>
> The templated function writes a long to a binary stream in 64 bits.
> This code works for 32 and 64 bit long.
>
> Thanks!
>
> Chris
>
> //===== This code is messy, allows for user error, but compiles =====
>
> #include <fstream>
>
> template<typename Type, int NumBits>
> void BinaryWrite64Bit(Type Value, std:stream& Stream);
>
> template<>
> void BinaryWrite64Bit<long, 32>(long Value, std:stream& Stream)
> {
> static const long Zero = 0;
> Stream.write(reinterpret_cast<const char*>(&Zero), sizeof(Zero));
> Stream.write(reinterpret_cast<const char*>(&Value), sizeof(Value));
> }
>
> template<>
> void BinaryWrite64Bit<long, 64>(long Value, std:stream& Stream)
> {
> Stream.write(reinterpret_cast<const char*>(&Value), sizeof(Value));
> }
>
> int main()
> {
> std:fstream Stream;
>
> long Value = 10;
>
> BinaryWrite64Bit<long, sizeof(long)*8>(Value, Stream);
>
> return 0;
> }
>
> //===== This code is clean, robust but doesn't compile =====
> #include <fstream>
>
> template<typename Type, int NumBits = sizeof(Type)*8>
> void BinaryWrite64Bit(Type Value, std:stream& Stream);
>
> template<>
> void BinaryWrite64Bit<long, 32>(long Value, std:stream& Stream)
> {
> static const long Zero = 0;
> Stream.write(reinterpret_cast<const char*>(&Zero), sizeof(Zero));
> Stream.write(reinterpret_cast<const char*>(&Value), sizeof(Value));
> }
>
> template<>
> void BinaryWrite64Bit<long, 64>(long Value, std:stream& Stream)
> {
> Stream.write(reinterpret_cast<const char*>(&Value), sizeof(Value));
> }
>
> int main()
> {
> std:fstream Stream;
>
> long Value = 10;
>
> BinaryWrite64Bit(Value, Stream);
>
> return 0;
> }


How about using a dispatcher to automatically select an implementation:

template<typename Type, int NumBits>
void BinaryWrite64BitImpl(Type, std:stream&);

template<>
void BinaryWrite64BitImpl<long, 32>(
const long value,
std:stream& stream)
{
// ...
}

template<>
void BinaryWrite64BitImpl<long, 64>(
const long value,
std:stream& stream)
{
// ...
}

template<typename Type>
void BinaryWrite64Bit(
const Type value,
std:stream& stream)
{
BinaryWrite64BitImpl
<
Type,
sizeof(Type)*CHAR_BIT
>( value, stream );

}

int main()
{
std:fstream Stream;
const long Value = 10;
BinaryWrite64Bit(Value, Stream);
return 0;
}

Cheers! --M

 
Reply With Quote
 
 
 
 
chrisstankevitz@yahoo.com
Guest
Posts: n/a
 
      11-09-2006

mlimber wrote:
> How about using a dispatcher to automatically select an implementation:


Wow, that is cool, never saw that before. Thanks mlimber!

I'm off to see where CHAR_BIT is defined (never saw that either)...

Chris

 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      11-09-2006

(E-Mail Removed) wrote:
> mlimber wrote:
> > How about using a dispatcher to automatically select an implementation:

>
> Wow, that is cool, never saw that before. Thanks mlimber!
>
> I'm off to see where CHAR_BIT is defined (never saw that either)...
>
> Chris


Yep, cool.
You'll find it in climits typically, although you might need to dig
down to limits.h

/* # of bits in a char */
# define CHAR_BIT 8

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      11-09-2006
(E-Mail Removed) wrote:
> Apparently default template arguments can only be used in classes. I
> include two apps below. One works but is IMO messy. The other does
> not work and is IMO clean.
>
> Q1: Why would c++ dissallow the clean version?


Apparently there is no good reason.

> Q2: Is there a way to do what I want to do "cleanly" (as I define it


Yes - compile your program with a C++0x compiler (may require a trip to
the future). The next major version of C++ does allow function
templates to specify default template type parameters.

Greg

 
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
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
Template argument as template argument nw C++ 0 04-14-2008 01:36 PM
Design issue : "self type" as a default template argument (recursive template arguments) IR C++ 3 11-22-2006 08:38 PM
an idiot question about a disallowed entity lkrubner@geocities.com XML 9 10-31-2005 05:18 AM
Disallowed Parent Path error Hunsal ASP .Net 1 05-20-2004 11:33 AM



Advertisments