2012-12-19 33 views
0

Possible Duplicate:
Clang, std::shared_ptr and std::less/operator<的unique_ptr中的std ::集找不到操作<儘管它已經在那裏

所以是的,標題是相當多問題的全部。正如你可以從下面的代碼片段看到的,我確實實現了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(); 
} 

和錯誤信息是:

In file included from ../llvm_opt_pass/cgd.cpp:3: In file included from /srv/scratch/sigubufo/clang/stable/3.1_src/llvm/include/llvm/Instructions.h:23: In file included from /srv/scratch/sigubufo/clang/stable/3.1_src/llvm/include/llvm/ADT/ArrayRef.h:13: In file included from /srv/scratch/sigubufo/clang/stable/3.1_src/llvm/include/llvm/ADT/SmallVector.h:24: In file included from /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: error: no matching function for call to object of type 'std::less<_CT>' return std::less<_CT>()(__x.get(), __y.get()); ^~~~~~~~~~~~~~~~

/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/stl_function.h:237:20: note: in instantiation of function template specialization 'std::operator<<::StateMachine::Transition, std::default_delete<::StateMachine::Transition>, ::StateMachine::Transition, std::default_delete<::StateMachine::Transition> >' requested here { return __x < __y; } ^

/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/stl_tree.h:1285:13: note: in instantiation of member function 'std::less::StateMachine::Transition, std::default_delete<::StateMachine::Transition> > >::operator()' requested here __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: > in instantiation of function template specialization '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> > >' requested here _M_t._M_insert_unique(std::move(__x)); ^

../llvm_opt_pass/cgd.cpp:72:17: note: in instantiation of member function '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> > > >::insert' requested here transitions.insert(move(new_t)); ^

/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/stl_function.h:236:7: note: candidate function not viable: no known conversion from 'pointer' (aka '::StateMachine::Transition *') to '::StateMachine::Transition *&&&' > for 1st argument; operator()(const _Tp& __x, const _Tp& __y) const

回答

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; 
+0

這是行不通的,因爲RHS必須引用同一個對象,因此違反了unique_ptr的意圖。 –

相關問題