首页 文章

std :: set中的unique_ptr找不到运算符<eventhough它在那里[重复]

提问于
浏览
0

可能重复:Clang,std :: shared_ptr和std :: less / operator <

所以,是的, Headers 几乎是整个问题 . 正如你从下面的片段中看到的那样,我确实实现了 operator< 所以我不知道发生了什么 .

这是代码:

namespace {

struct Transition {
    string name;
    StatePtr toState;

    Transition(string s = string(), StatePtr state = nullptr)
      : name(move(s))
      , toState(move(state))
    {}

    friend bool operator==(Transition const& lhs, Transition const & rhs) {
      return lhs.name == rhs.name && lhs.toState == rhs.toState;
    }

    friend bool operator<(Transition const & lhs, Transition const & rhs);
  };

  struct State {
    string name;
    set<TransitionPtr> transitions;

    explicit State(string s = string())
      : name(move(s))
    {}

    void addTransition(string s, StatePtr sp = nullptr){
      TransitionPtr new_t = make_transition(s, sp);
      for(auto& t : transitions){
        if(t == new_t){
          return;
        }
      }

      transitions.insert(move(new_t)); // This is where the error happens.
    }

  };
}

bool operator<(StateMachine::Transition const & lhs, StateMachine::Transition const & rhs) {
 return lhs.toState.get() < rhs.toState.get();
}

并且错误消息是:

在../llvm_opt_pass/cgd.cpp:3中包含的文件中:/srv/scratch/sigubufo/clang/stable/3.1_src/llvm/include/llvm/Instructions.h:23中包含的文件:包含在文件中/srv/scratch/sigubufo/clang/stable/3.1_src/llvm/include/llvm/ADT/ArrayRef.h:13:包含在/srv/scratch/sigubufo/clang/stable/3.1_src/llvm/include/中的文件中llvm / ADT / SmallVector.h:24:包含在/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c /4.7/memory:85的文件中:/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c /4.7/bits/unique_ptr.h:486:14:错误:没有匹配功能调用'std :: less <CT>'类型的对象返回std :: less <CT>()( x.get(),__ y.get()); ^ ~~~~~~~~~~~~~~~ / usr / lib / gcc / x86_64-linux-nuu / 4.7 / ... / .. / .. / .. / include / c /4.7/bits /stl_function.h:237:20:注意:在实例化函数模板特化'std :: operator << :: StateMachine :: Transition,std :: default_delete <:: StateMachine :: Transition>,:: StateMachine :: Transition ,std :: default_delete <:: StateMachine :: Transition >>>'request here {return __x <__y; } ^ /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c /4.7/bits/stl_tree.h:1285:13:注意:在实例化中成员函数'std :: less :: StateMachine :: Transition,std :: default_delete <:: StateMachine :: Transition >> >> :: operator()'此处请求__comp = M_impl.M_key_compare(KeyOfValue()( v), S_key (X)); ^ /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c /4.7/bits/stl_set.h:424:9:注意:>实例化函数模板特化'std :: _ Rb_tree :: StateMachine :: Transition,std :: default_delete <:: StateMachine :: Transition >>,std :: unique_ptr <:: StateMachine :: Transition,std :: default_delete <:: StateMachine: :Transition >>,std :: _ Identity :: StateMachine :: Transition,std :: default_delete <:: StateMachine :: Transition >>>,std :: less :: StateMachine :: Transition,std :: default_delete <:: StateMachine :: Transition >> >>,std :: allocator :: StateMachine :: Transition,std :: default_delete <:: StateMachine :: Transition >>> :: _ M_insert_unique :: StateMachine :: Transition,std :: default_delete <:: StateMachine :: Transition >> >>'在这里请求_M_t.M_insert_unique(std :: move( x)); ^ ../llvm_opt_pass/cgd.cpp:72:17:注意:在成员函数'std :: set :: StateMachine :: Transition,std :: default_delete <:: StateMachine :: Transition >>>的实例化中,std :: less :: StateMachine :: Transition,std :: default_delete <:: StateMachine :: Transition >>>,std :: allocator :: StateMachine :: Transition,std :: default_delete <:: StateMachine :: Transition >>>>: :插入'在这里请求transitions.insert(move(new_t)); ^ /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c /4.7/bits/stl_function.h:236:7:注意:候选功能不可行:对于第一个参数,没有已知的从'指针'(又名':: StateMachine :: Transition *')到':: StateMachine :: Transition * &&&'>的转换; operator()(const Tp& x,const _Tp&__y)const

2 回答

  • 1

    好的,所以解决方案与此问题相同:Clang, std::shared_ptr and std::less/operator< .

    基本上它是来自libstdc的type_traits中的一个错误 .

  • 0

    尝试提供自定义函子作为std :: less的替代 .

    struct cmp  {
        bool operator() ( const Transition& lhs, const Transition& rhs )  {
            return lhs.toState.get() < rhs.toState.get();
         }
     };
    
    std::set< Transition, cmp > transitions;
    

相关问题