template<size_t N>
void cram_helper( unsigned long long value, char* const dst )
{
char* p = dst + N;
while (p > dst) { *--p = value & 0xFF; value >>= 8; }
}
template<typename T>
void cram( T t, char* dst )
{
cram_helper<sizeof(T)>(t, dst);
}
char buffer[] = { "abcdefg" };
short s = 25;
cram(s, buffer+1);
string buffer1 = "abcdefg";
cram(s, &buffer[1]);
template<size_t N, typename Container>
void cram_helper( unsigned long long value, Container& dst, int initial_index )
{
char index = initial_index + N;
while (index > initial_index) { dst[--index] = value & 0xFF; value >>= 8; }
}
template<typename T, typename Container>
void cram( T t, Container& dst, size_t initial_index = 0 )
{
cram_helper<sizeof(T)>(t, dst, initial_index);
}
std::string buffer2 = "abcdefg";
short s = 25;
cram(s, buffer2, 1);