I'm playing around with an idea and I came across an issue that I'm really scratching my head about. I was hoping someone could give me a clear (preferably cited) explanation before I start migrating this idea into a larger framework.
I'm running the following experiment:
/*
* gcc -Wall -O0 -g -std=c++11 main.cc -lstdc++ -lm -o mathras
* clang++ -Wall -O0 -g -std=c++11 main.cc -lstdc++ -lm -o mathras
*/
#include <type_traits>
#include <functional>
template<typename U, typename V>
struct is_comparable {
static constexpr bool value = (std::is_integral<U>::value && std::is_integral<V>::value) || (std::is_floating_point<U>::value && std::is_floating_point<V>::value);
};
template<typename T>
class interval {
public:
inline interval();
inline interval(const T& x);
inline interval(const T& min, const T& max);
public:
template<typename U>
constexpr typename std::enable_if<!is_comparable<T, U>::value, bool>::type
operator==(const interval<U>&) const noexcept;
template<typename U>
inline typename std::enable_if<is_comparable<T, U>::value, bool>::type
operator==(const interval<U>&) const noexcept;
inline bool operator!=(const T& rhs) const noexcept;
template<typename U>
constexpr typename std::enable_if<!is_comparable<T, U>::value, bool>::type
operator!=(const interval<U>&) const noexcept;
template<typename U>
inline typename std::enable_if<is_comparable<T, U>::value, bool>::type
operator!=(const interval<U>& rhs) const noexcept;
public:
inline T& max() noexcept;
inline T const& max() const noexcept;
inline T& min() noexcept;
inline T const& min() const noexcept;
protected:
T min_;
T max_;
};
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char** argv) {
interval<int> a(0, 25);
interval<unsigned> b(0, 25);
interval<float> c(0, 25);
interval<double> d(0, 25);
cout << boolalpha <<
"a == b = " << (a == b) << endl <<
"a != b = " << (a != b) << endl <<
"a == c = " << (a == c) << endl <<
"a != c = " << (a != c) << endl <<
"a == d = " << (a == d) << endl <<
"a != d = " << (a != d) << endl <<
"b == c = " << (b == c) << endl <<
"b != c = " << (b != c) << endl <<
"b == d = " << (b == d) << endl <<
"b != d = " << (b != d) << endl <<
"c == d = " << (c == d) << endl <<
"c != d = " << (c != d) << endl <<
flush;
return 0;
};
template<typename T>
inline interval<T>::interval():
min_(),
max_()
{ };
template<typename T>
inline interval<T>::interval(const T& x):
interval(x, x)
{ };
template<typename T>
inline interval<T>::interval(const T& min, const T& max):
min_((max < min) ? max : min),
max_((min > max) ? min : max)
{ };
template<typename T>
inline T& interval<T>::max() noexcept {
return this->max_;
};
template<typename T>
inline T const& interval<T>::max() const noexcept {
return this->max_;
};
template<typename T>
inline T& interval<T>::min() noexcept {
};
template<typename T>
inline T const& interval<T>::min() const noexcept {
return this->min_;
};
template<typename T>
template<typename U>
constexpr typename std::enable_if<!is_comparable<T, U>::value, bool>::type
interval<T>::operator==(const interval<U>&) const noexcept {
return false;
};
template<typename T>
template<typename U>
inline typename std::enable_if<is_comparable<T, U>::value, bool>::type
interval<T>::operator==(const interval<U>& rhs) const noexcept {
return (this->min_ == rhs.min()) && (this->max_ == rhs.max());
};
template<typename T>
template<typename U>
constexpr typename std::enable_if<!is_comparable<T, U>::value, bool>::type
interval<T>::operator!=(const interval<U>&) const noexcept {
return true;
};
template<typename T>
template<typename U>
inline typename std::enable_if<is_comparable<T, U>::value, bool>::type
interval<T>::operator!=(const interval<U>& rhs) const noexcept {
return (this->min_ != rhs.min()) || (this->max_ != rhs.max());
};
As you can see, it seems pretty straight forward stuff. is_comparable determines if two numbers for an interval are comparable, and then the class interval uses that via enable_if to specialize the comparison.
Neither gcc, nor vc++ (according to http://webcompiler.cloudapp.net/) complain. They both warn (appropriately and expectedly) about unsigned vs signed comparison, but go on to compile, link, and produce the expected output.
Here's what's got me scratching my head. Clang does not complain about signedness. Instead, it produces the following warnings...
main.cc:24:4: warning: inline function 'interval<int>::operator==<float>' is not defined [-Wundefined-inline]
operator==(const interval<U>&) const noexcept;
^
main.cc:57:22: note: used here
"a == c = " << (a == c) << endl <<
^
main.cc:31:4: warning: inline function 'interval<int>::operator!=<float>' is not defined [-Wundefined-inline]
operator!=(const interval<U>&) const noexcept;
^
main.cc:58:22: note: used here
"a != c = " << (a != c) << endl <<
^
main.cc:24:4: warning: inline function 'interval<int>::operator==<double>' is not defined [-Wundefined-inline]
operator==(const interval<U>&) const noexcept;
^
main.cc:59:22: note: used here
"a == d = " << (a == d) << endl <<
^
main.cc:31:4: warning: inline function 'interval<int>::operator!=<double>' is not defined [-Wundefined-inline]
operator!=(const interval<U>&) const noexcept;
^
main.cc:60:22: note: used here
"a != d = " << (a != d) << endl <<
^
main.cc:24:4: warning: inline function 'interval<unsigned int>::operator==<float>' is not defined [-Wundefined-inline]
operator==(const interval<U>&) const noexcept;
^
main.cc:61:22: note: used here
"b == c = " << (b == c) << endl <<
^
main.cc:31:4: warning: inline function 'interval<unsigned int>::operator!=<float>' is not defined [-Wundefined-inline]
operator!=(const interval<U>&) const noexcept;
^
main.cc:62:22: note: used here
"b != c = " << (b != c) << endl <<
^
main.cc:24:4: warning: inline function 'interval<unsigned int>::operator==<double>' is not defined [-Wundefined-inline]
operator==(const interval<U>&) const noexcept;
^
main.cc:63:22: note: used here
"b == d = " << (b == d) << endl <<
^
main.cc:31:4: warning: inline function 'interval<unsigned int>::operator!=<double>' is not defined [-Wundefined-inline]
operator!=(const interval<U>&) const noexcept;
^
main.cc:64:22: note: used here
"b != d = " << (b != d) << endl <<
^
Based on the warnings, it would seem that it's not complaining about anything having to with interval<U>
, but instead U
itself, unless that's just a quirk in it's warning output.
Regardless, it compiles, but when linked, errors out on the undefined references it failed to inline in the first place.
I tried it with clang 3.3 and 3.4 with the same results.
First question. Why? My code looks good to me (ignoring expected stuff like signedness). Am I actually making a major mistake somewhere which clang is picking up on (and they other compilers are actually buggy about), or is clang simply buggy about this?
Second question. Regardless of why, what is the most cross-compiler appropriate way to accomplish this task?
main
but making sure the definitions appear before they are used is of course better.