C
chrisstankevitz
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;
}
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;
}