I’m creating a high precision floating point library (for educational purpose) and the constructors are shown below.
The idea is to have a generic constructor (the first one) and then 2 specialized constructors for std::string and std::bitset.
The problem is that I keep getting: “error C2975: ‘Float’ : invalid template argument for ‘N’, expected compile-time constant expression” for both specialized constructors.
Can anyone tell me how to correct this?
Thanks in advance!
template<typename T>
Float(T n) {
type_wrapper<T> data;
data.in = n;
bits = std::bitset<N>(data.out);
_overflow = false;
}
template<>
Float< const std::bitset<N> >(const std::bitset<N> bits) {
this->bits = bits;
_overflow = false;
}
template<>
Float< const std::string& >(const std::string& s) {
int n = std::min(N, s.length());
for(int i = 0; i < n; i++) {
bits[n-i-1] = (s.at(i) == '1' ? 1 : 0);
}
_overflow = false;
}
EDIT: I should probably add that I’m actually constructing a Float object with a constant size like this:
int main(int argc, char* argv[])
{
Float<64> number("01011001100010001110010101100111"); //1502143847
std::cout << number << std::endl;
std::cin.get();
return 0;
}
Also the templated argument ‘N’ is from the class definition like so:
template<size_t N>
class Float
{
private:
...
public:
...
};
Don’t specialize your constructor, use overloading.
The original error is probably because you don’t fully qualify your constructs when defining them. This should be: