Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ g++;和叮当声++;与操作员的不同行为<;()过载_C++_C++11_Operator Overloading_Implicit Conversion - Fatal编程技术网

C++ g++;和叮当声++;与操作员的不同行为<;()过载

C++ g++;和叮当声++;与操作员的不同行为<;()过载,c++,c++11,operator-overloading,implicit-conversion,C++,C++11,Operator Overloading,Implicit Conversion,你好,很抱歉我的英语不好 为了练习c++11,我尝试编写一个版本的类std::experimental::any(),添加一些额外的内容 添加运算符decltype(std::declval()) isLess(a0.ptr.get()); } 公众: 模板 任何(T&&v0) :ptr(新的anyD(std::forward(v0))) { } bool empty()常量noexcept {return!bool(ptr);} std::type_info const和type()const

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

为了练习c++11,我尝试编写一个版本的类std::experimental::any(),添加一些额外的内容

添加运算符decltype(std::declval()) bool {throw std::runtime_错误(“无运算符<类型”);} 公众: anyD(T常数和v0) :val(v0) { } std::type_info const&typeT()const override final {返回typeInfo();} 布尔无岛(anyB const*pB0)const override final { 自动pD0=动态广播(pB0); 如果(nullptr==pD0) 抛出std::bad_cast(); 返回lessF(val,pD0->val,0); } }; 任何类别 { 私人: 模板 使用sT=typename std::decay::type; 模板 使用noAny =typename std::启用\u如果 ::类型; 模板 使用isCpCtr =typename std::启用\u如果 ::类型; std::唯一的ptr ptr; 静态标准::类型信息常量和无效信息() {static auto const&ret=typeid(void);返回ret;} bool opLess(任意常数和a0)常数 { 返回 类型()在(a0.type()之前) ||((type()==a0.type()) &&(false==empty()) &&ptr.get()->isLess(a0.ptr.get()); } 公众: 模板 任何(T&&v0) :ptr(新的anyD(std::forward(v0))) { } bool empty()常量noexcept {return!bool(ptr);} std::type_info const和type()const {return(ptr?ptr->typeT():voidInfo());} 友元布尔运算符<(任意常数&,任意常数&); }; 布尔运算符<(任意常数和a0,任意常数和a1) {返回a0.oppress(a1);} } int main() { 尝试 { yans::任何ai{12}; yans::anyas{std::string(“t1”)}; yans::任意au{std::无序_集{1,5,3};
std::cout我相信您已经在gcc中发现了一个bug(我已经以更短的形式复制了它,请继续关注)


问题是,如果你仔细查看分段错误,你会发现
操作符@Barry,我认为你是对的:这是一个可见性问题。我在虚拟类定义(anyB)
classany;
布尔操作符<(any const&a0,any const&a1)前面放了以下几行
现在,由clang++生成的程序进入循环并生成分段错误。祝贺你的示例:非常简单。@Barry,但关于你的解决方案……好吧……以你的方式,我失去了将任何类的实例与泛型类型T进行比较的能力;就像示例程序一样。我认为这将是应用程序适当地放置显式模板构造函数(并在任何对象遇到通用T对象时为操作符开发模板专门化)。但是,在std::experience::any的规范中,模板构造函数不是显式的。@max66如果你按照我的建议去做,你仍然能够比较
any
和非
any
的模板构造函数。ADL会找到正确的
操作符@Batty:你完全正确。我有一个错误,但我犯了一个非常愚蠢的错误。谢谢Barry。
#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;
 }
---- 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 ----
class any {
    friend bool operator< (any const & a0, any const & a1) {
        return a0.opLess(a1);
    }
};