std :: set中的unique_ptr找不到运算符<即使它在那里

时间:2012-12-19 13:14:50

标签: c++ c++11 clang unique-ptr

  

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

所以是的,标题几乎就是整个问题。正如你从下面的代码片段中看到的那样,我确实实现了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&lt; _CT&gt;'类型的对象         return std :: less&lt; _CT&gt;()(__ x.get(),__ y.get());                ^ ~~~~~~~~~~~~~~~

     

/usr/lib/gcc/x86_64-linux-gnu/4.7 /../../../../ include / c ++ / 4.7 / bits / stl_function.h:237:20:注意:在实例化中函数模板特化'std :: operator&lt;&lt; :: StateMachine :: Transition,std :: default_delete&lt; :: StateMachine :: Transition&gt;,:: StateMachine :: Transition,std :: default_delete&lt; :: StateMachine :: Transition&gt ; &GT;”这里要求         {return __x&lt; __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&lt; :: StateMachine :: Transition&gt; &GT;这里请求&gt; :: 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:note:&gt;在实例化函数模板特化'std :: _ Rb_tree :: StateMachine :: Transition,std :: default_delete&lt; :: StateMachine :: Transition&gt; &gt;,std :: unique_ptr&lt; :: StateMachine :: Transition,std :: default_delete&lt; :: StateMachine :: Transition&gt; &gt;,std :: _ Identity :: StateMachine :: Transition,std :: default_delete&lt; :: StateMachine :: Transition&gt; &GT; &gt;,std :: less :: StateMachine :: Transition,std :: default_delete&lt; :: StateMachine :: Transition&gt; &GT; &gt;,std :: allocator :: StateMachine :: Transition,std :: default_delete&lt; :: StateMachine :: Transition&gt; &GT; &GT; :: _ M_insert_unique :: StateMachine :: Transition,std :: default_delete&lt; :: StateMachine :: Transition&gt; &GT; &GT;”这里要求             _M_t._M_insert_unique(标准::移动(__ X));                  ^

     

../ llvm_opt_pass / cgd.cpp:72:17:注意:在成员函数'std :: set :: StateMachine :: Transition,std :: default_delete&lt; :: StateMachine :: Transition&gt;的实例化中&gt;,std :: less :: StateMachine :: Transition,std :: default_delete&lt; :: StateMachine :: Transition&gt; &GT; &gt;,std :: allocator :: StateMachine :: Transition,std :: default_delete&lt; :: StateMachine :: Transition&gt; &GT; &GT; &gt; ::插入'在这里请求                                   transitions.insert(移动(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 *&amp;&amp;&amp;&amp;' &GT;第一个论点;         operator()(const _Tp&amp; __x,const _Tp&amp; __y)const

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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;