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