g++ 和 clang++ 与运算符<() 重载的不同行为
g++ and clang++ different behaviour with operator<() overloading
你好,对不起我的英语不好。
为了练习 c++11,我正在尝试编写一个 std::experimental::any (http://en.cppreference.com/w/cpp/experimental/any) 类的版本,添加一些额外的东西。
添加运算符<() 我在 g++ (4.9.2) 和 clang++ (3.5.0) 之间得到了不同的行为。
以下是该类(以及使用的类)的缩短版本,涵盖了触发问题的最小必要值和非常小的 main()。
很抱歉代码很长,但我未能缩短示例。
#include <memory>
#include <iostream>
#include <type_traits>
#include <unordered_set>
namespace yans // yet another name space
{
class anyB // base for any
{
public:
virtual std::type_info const & typeT () const = 0;
virtual bool isLess (anyB const *) const = 0;
};
template <typename T>
class anyD : public anyB // derived for any
{
private:
T val;
static std::type_info const & typeInfo ()
{ static auto const & ret = typeid(T); return ret; }
template <typename U> // preferred version
static auto lessF (U const & u1, U const & u2, int)
-> decltype( std::declval<U const &>()
< std::declval<U const &>())
{ return (u1 < u2); }
template <typename U> // emergency version
static auto lessF (U const &, U const &, ...) -> bool
{ throw std::runtime_error("no operator < for type "); }
public:
anyD (T const & v0)
: val(v0)
{ }
std::type_info const & typeT () const override final
{ return typeInfo(); }
bool isLess (anyB const * pB0) const override final
{
auto pD0 = dynamic_cast<anyD<T> const *>(pB0);
if ( nullptr == pD0 )
throw std::bad_cast();
return lessF(val, pD0->val, 0);
}
};
class any
{
private:
template <class T>
using sT = typename std::decay<T>::type;
template <class T>
using noAny
= typename std::enable_if
<false == std::is_same<any, sT<T>>::value, bool>::type;
template <class T>
using isCpCtr
= typename std::enable_if
<true == std::is_copy_constructible<sT<T>>::value,bool>::type;
std::unique_ptr<anyB> ptr;
static std::type_info const & voidInfo ()
{ static auto const & ret = typeid(void); return ret; }
bool opLess (any const & a0) const
{
return
type().before(a0.type())
|| ( (type() == a0.type())
&& (false == empty())
&& ptr.get()->isLess(a0.ptr.get()) );
}
public:
template <typename T, typename = noAny<T>, typename = isCpCtr<T>>
any (T && v0)
: ptr(new anyD<sT<T>>(std::forward<T>(v0)))
{ }
bool empty () const noexcept
{ return ! bool(ptr); }
std::type_info const & type () const
{ return ( ptr ? ptr->typeT() : voidInfo()); }
friend bool operator< (any const &, any const &);
};
bool operator< (any const & a0, any const & a1)
{ return a0.opLess(a1); }
}
int main ()
{
try
{
yans::any ai { 12 };
yans::any as { std::string("t1") };
yans::any au { std::unordered_set<int> { 1, 5, 3 } };
std::cout << "ai < 13 ? " << (ai < 13) << 'n';
std::cout << "as < std::string {"t0"} ? "
<< (as < std::string {"t0"}) << 'n';
std::cout << "au < std::unordered_set<int> { 2, 3, 4 } ? "
<< (au < std::unordered_set<int> { 2, 3, 4 }) << 'n';
}
catch ( std::exception const & e )
{
std::cerr << "nmain(): standard exception of type ""
<< typeid(e).name() <<""n"
<< " ---> " << e.what() << " <---nn";
}
return EXIT_SUCCESS;
}
运算符<()背后的思想是,如果左操作数的类型小于右操作数的类型(根据typeid(T).before()),则返回"true",如果类型匹配,则返回通过比较包含的值返回的值。我知道这是一个有问题的解决方案,但我是在玩学习。
麻烦的是,在类的实例中,任何都可以包含没有运算符<()的类型值。在示例中,类 std::unordered_set
使用 clang++,我得到了我想要的:类 anyD<std::unordered_set>
相反,使用 g++,类 anyD<std::unordered_set>
我想了解的是:
根据 ISO c++11,clang++ 的行为或 g++ 的行为是否正确?
我是否可以在本地(仅在 lessF() 中)并且不声明 any() 的模板构造函数"显式"),两个 std::unordered_set
之间的比较会在两个 any 之间的对抗中转向自己?换句话说:我怎样才能防止在anyD<std::unordered_set> >中开发lessF()的优先版本?
以下是两个程序的输出。
---- clang++ program output ----
ai < 13 ? 1
as < std::string {"t0"} ? 0
au < std::unordered_set<int> { 2, 3, 4 } ?
main(): standard exception of type "St13runtime_error"
---> no operator < for type <---
---- end output ----
---- g++ program output ----
ai < 13 ? 1
as < std::string {"t0"} ? 0
Errore di segmentazione
---- end output ----
我相信你已经在 gcc 中发现了一个错误(我在这里以更短的形式复制了它,敬请期待)。
问题是,如果你查看分割错误,你会发现unordered_set<int>
的operator<
调用是无限递归的。这是因为 gcc 实际上认为bool operator<(const any&, const any&)
是匹配的。它不应该在你调用它的地方。
简单的解决方法是简单地确保只为any
找到operator<(const any&, const any&)
,无论您在哪个命名空间中。只需将定义移动到类中:
class any {
friend bool operator< (any const & a0, any const & a1) {
return a0.opLess(a1);
}
};
无论如何,这是很好的做法。
- 错误:使用 clang 没有可行的重载,使用 GCC 编译
- clang++ 8.0.1 自分配重载警告
- clang 拒绝具有尾随 decltype 返回类型的模板调用是否正确,具体取决于其重载之一?
- 菱形层次结构中的虚函数重载在 clang 和 gcc 中产生不同的结果
- Clang 中的运算符重载不明确
- 线性重载:为什么 clang 在 GCC 编译时失败?
- clang-libc++错误:重载解析选择了隐式删除的复制赋值运算符
- g++ 和 clang++ 与运算符<() 重载的不同行为
- 函数模板重载 clang++
- c++重载了clang发出的虚拟函数警告
- 重载抽象运算符=时出现Clang链接器错误
- g++和clang++-删除由重载转换运算符歧义获取的指针
- 编写具有枚举基但仅具有clang的枚举时存在不明确的重载
- enable_if的C++模板重载:g++和clang的行为不同
- GCC 和 Clang 在运算符重载解析期间隐式实例化模板参数
- c++ 11构造函数重载解析和initialiser_lists: clang++和c++不一致.< / h1 &
- GCC vs clang -使用' make_overload '可变lambda继承时的模糊重载
- 重载解析在gcc和clang之间得到不同的结果
- GCC声明友元函数是重载的,有歧义调用,clang编译
- 操作符重载clang++和g++不同的输出