I'm working with a set of classes and my main code looks like this:
main.cpp
#include "calc.h"
int main() {
neg_inf nif;
pos_inf pif;
limit<double, infinity> l( 3.4, nif, pif, 2.2 )
std::cout << "value dx = " << l.value() << '\n'
<< "lower lim = " << l.lower() << '\n'
<< "upper lim = " << l.upper() << '\n'
<< "step_size = " << l.step() << '\n';
return EXIT_SUCCESS;
}
The expected output should be:
value dx = 3.4
lower lim = -inf
upper lim = inf
step_size = 2.2
Here are my classes:
calc.h
#pragma once
#include <cmath>
#include <iostream>
#include <limits>
#include <type_traits>
struct infinity {
protected:
infinity() = default;
};
struct pos_inf : public infinity {
constexpr double operator()() { return std::numeric_limits<double>::infinity(); }
};
struct neg_inf : public infinity {
constexpr double operator()() { return -std::numeric_limits<double>::infinity(); }
};
std::ostream& operator<<( std::ostream& os, const pos_inf& inf );
std::ostream& operator<<( std::ostream& os, const neg_inf& inf );
template<typename dX, class bound>
class limit {
dX dx;
bound lowerBound;
bound upperBound;
double step_size;
public:
limit( dX x, bound lower, bound upper, double step = 1 ) :
dx{ x }, lowerBound{ lower }, upperBound { upper }, step_size { step }
{}
dX value() const { return dx; }
bound lower() const { return lowerBound; }
bound upper() const { return upperBound; }
double step() const { return step_size; }
};
calc.cpp
#include "calc.h"
std::ostream& operator<<( std::ostream& os, const pos_inf& inf ) {
// originally intended to do:
// return os << inf(); // but fails to compile
auto v = pos_inf()(); // this works
return os << v;
}
std::ostream& operator<<( std::ostream& os, const neg_inf& inf ) {
// same as above...
auto v = neg_inf()();
return os << v;
}
However in the main.cpp Visual Studio 2017 is generating this compiler error:
c:\***\main.cpp(33): error C2679: binary '<<': no operator found which takes a right-hand operand of type 'bound' (or there is no acceptable conversion)
1> with
1> [
1> bound=infinity
1> ]
based on this line of code:
<< "lower lim = " << l.lower() << '\n'
and is failing from l.lower()
However if I do this in main:
#include "calc.h"
int main() {
neg_inf nif;
pos_inf pif;
std::cout << nif << '\n' << pif << '\n'
return EXIT_SUCCESS;
}
I am getting the output:
-inf
inf
This tells me that my operator<<()
are working for the inherited structs, however when I pass it's parent type as a template argument and pass the derived types into the constructor of my limit
class, the operator<<()
are not resolving. It appears to be an ambiguity problem but I'm not sure how to resolve this. What am I missing or overlooking here?
As a side note which is outside of this question, is there a more elegant way to represent -/+inf
? I'm using inheritance here because +
and -
inf
are not numbers but more of a concept, they are similar to each other but point in different directions. So when I pass an infinity type as a template argument I'd like to be able to set the lower bound to -inf and the upper bound to +inf. I want the bound type to be a template because I might want to use integer bounds or double bounds for example between [-1,1]
or [0.0,1.0]
in which these are all numeric bounds. I'm not sure how else to express infinity in a more elegant way and any tips or suggestions will be appreciated.