【问题标题】:g++ and clang++ different behaviour with operator<() overloadingg++ 和 clang++ 使用 operator<() 重载的不同行为
【发布时间】:2016-06-19 11:50:39
【问题描述】:

你好,很抱歉我的英语不好。

为了练习 c++11,我正在尝试编写 std::experimental::any (http://en.cppreference.com/w/cpp/experimental/any) 类的一个版本,添加一些额外的东西。

添加运算符

以下是该类(以及使用的类)的简化版本,涵盖了最低限度的必要条件,以及触发问题的非常小的 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() << " <---\n\n";
    }

   return EXIT_SUCCESS;
 }

operator

问题在于,在类的实例中,任何类型的值都可以包含在没有运算符 的实例。然后我尝试开发几个重载(SFINAE)方法 lessF();首选,当 operator

使用 clang++,我得到了我想要的:类 anyD<:unordered_set>> 没有实现 lessF 的首选版本,并且比较从紧急版本生成异常。

使用 g++,相反,类 anyD<:unordered_set>> 生成优先版本,在 any 的两个实例上调用 operator lessF() 的参数(any 的模板化构造函数没有“显式”定义),然后递归调用自身,进入循环并生成错误(“Errore di segmentazione”,即“分段错误”)。

我想了解的是:

  • 根据 ISO c++11,clang++ 的行为正确还是 g++ 的行为正确?

  • 我能否在本地(仅在 lessF() 中)防止两个 std::unordered_set 之间的比较将在对抗中转为“显式”两者之间有什么?换句话说:如何防止在 anyD<: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 ----

【问题讨论】:

    标签: c++ c++11 operator-overloading implicit-conversion


    【解决方案1】:

    我相信您已经发现了 gcc 中的一个错误(我以更短的形式复制了它here,敬请期待)。

    问题是,如果您查看分段错误,您会发现operator&lt;unordered_set&lt;int&gt; 的调用是无限递归的。这是因为 gcc 实际上认为 bool operator&lt;(const any&amp;, const any&amp;) 是匹配的。它不应该在你调用它的地方。

    简单的解决方法是确保operator&lt;(const any&amp;, const any&amp;)any 找到,无论您在哪个命名空间中。只需将定义移动到类中: p>

    class any {
        friend bool operator< (any const & a0, any const & a1) {
            return a0.opLess(a1);
        }
    };
    

    无论如何,这是一个很好的做法。

    【讨论】:

    • @Barry,我认为你是对的:这是一个可见性问题。我将以下几行放在虚拟类定义(anyB)class any;bool operator&lt; (any const &amp; a0, any const &amp; a1); 之前,现在由 clang++ 生成的程序进入循环并生成分段错误。祝贺你的例子:非常简单。
    • @ Barry,但是关于你的解决方案......好吧......以你的方式,我失去了将任何类的实例与泛型类型 T 进行比较的能力;如示例程序中一样。我认为放置显式模板构造函数(并在任何对象遇到通用 T 对象时为运算符开发模板特化)是合适的。但是,在 std::experiment::any 的规范中,模板构造函数不是显式的。
    • @max66 如果您按照我的建议进行操作,您仍然可以将any 与非any 进行比较。 ADL 会找到正确的operator &lt; 并执行转换。
    • @巴蒂:你说得对。我得到一个错误,但我犯了一个非常愚蠢的错误。谢谢巴里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多