我正在玩弄一个想法,我遇到了一个我真正摸不着头脑的问题。在我开始将这个想法迁移到更大的框架之前,我希望有人能给我一个明确的(最好引用的)解释。
我正在进行以下实验:
/*
* 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());
};
正如你所看到的,这看起来很简单。 is_comparable确定一个区间的两个数字是否具有可比性,然后类间隔通过enable_if使用它来专门化比较。
gcc和vc ++(根据http://webcompiler.cloudapp.net/)都不抱怨。他们都(无论是适当的还是预期的)警告无符号vs符号比较,但继续编译,链接并产生预期的输出。
这是让我挠头的原因。 Clang并没有抱怨签名。相反,它产生以下警告......
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 <<
^
基于这些警告,它似乎并没有抱怨任何与interval<U>
相关的内容,而是U
本身,除非这只是一个怪癖和&#39}} #39;警告输出。
无论如何,它都会编译,但是当链接时,未定义的引用上的错误首先无法内联。
我用clang 3.3和3.4尝试了相同的结果。
第一个问题。为什么?我的代码对我来说很好(忽略了像签名这样的预期内容)。我实际上是在某个地方犯了一个重大的错误,而且其他编译器实际上都是错误的,或者只是因为这个问题而吵吵嚷嚷?
第二个问题。无论为什么,交叉编译器最适合完成此任务的方法是什么?
答案 0 :(得分:5)
我一直在使用gcc waaaay太长时间(刚刚转到clang)。
警告告诉我它没有被定义,因为它已定义了WASN&T。它们定义在main()
以下。似乎gcc / vc ++进行了预扫描,因此它们是定义的,即使严格来说,它们也不应该被定义。
我刚刚将main()
移到了底部,瞧。
我想我只需要使用stackoverflow作为解决我自己问题的听板。