Z
ZikO
Hi
there's the simple template of the function which converts a number into
bits as below:
template<class T>
const std::string toBits(const T& n, const char& sep = '\0') {
size_t no_bits = sizeof(n)*8;
std::string bits;
for(size_t shift = no_bits; shift > 0; shift--) {
char ch = ((n>>(shift-1)) & 1) ? '1' : '0';
bits.push_back(ch);
if((no_bits > 8) && (((shift-1) % 8) == 0)) {
if(sep != '\0')
bits.push_back(sep);
}
}
return bits;
}
Now. I don't want the function to operate on double / float / long
double / bool etc. I've supplied the code with explicit specializations:
template<>
const std::string toBits<double>(const double& n, const char& sep = '\0') {
return string("It is not allowed to use bit operands on double type");
4 times for all four floating types. I cannot compile the code. The
compiler says for every specialization the same error:
error: default argument specified in explicit specialization
The second thing is I don't know how I could simply eliminate any other
types which is not char int or its derivative like short long etc. let's
say if someone try to pass object of X class I would like compiler to
use general specialization of the function which stays it's wrong type.
Regards.
there's the simple template of the function which converts a number into
bits as below:
template<class T>
const std::string toBits(const T& n, const char& sep = '\0') {
size_t no_bits = sizeof(n)*8;
std::string bits;
for(size_t shift = no_bits; shift > 0; shift--) {
char ch = ((n>>(shift-1)) & 1) ? '1' : '0';
bits.push_back(ch);
if((no_bits > 8) && (((shift-1) % 8) == 0)) {
if(sep != '\0')
bits.push_back(sep);
}
}
return bits;
}
Now. I don't want the function to operate on double / float / long
double / bool etc. I've supplied the code with explicit specializations:
template<>
const std::string toBits<double>(const double& n, const char& sep = '\0') {
return string("It is not allowed to use bit operands on double type");
4 times for all four floating types. I cannot compile the code. The
compiler says for every specialization the same error:
error: default argument specified in explicit specialization
The second thing is I don't know how I could simply eliminate any other
types which is not char int or its derivative like short long etc. let's
say if someone try to pass object of X class I would like compiler to
use general specialization of the function which stays it's wrong type.
Regards.