내가 std::bind
과를 rvalue 참조와 함께 연주하고,하지만 난 아직도, 나는 다음과 같은 코드가 작동하는 방법을 알아낼 수 없습니다 :표준 : 바인드() : 인수로를 rvalue 참조와 바인드 람다
class Dog {
public:
Dog(const string &name) : name_(name) {
cout << "Dog::ctor" << endl;
}
string GetName() {
return name_;
}
private:
string name_;
};
auto bind_fun = bind([](Dog &&d){ cout << d.GetName() << endl; }, Dog("DogABC"));
bind_fun();
을
을 주석으로 처리하거나 람다가 Dog&&
이 아닌 Dog&
을 사용하는 경우 코드가 예상 출력으로 올바르게 실행됩니다.
test3.cpp:109:3: error: no matching function for call to object of type 'std::__1::__bind<<lambda at test3.cpp:108:17>, Dog>'
f();
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:1749:9: note: candidate template ignored: substitution failure [with _Args = <>]: implicit instantiation of undefined template
'std::__1::__bind_return<<lambda at test3.cpp:108:17>, std::__1::tuple<Dog>, std::__1::tuple<>, false>'
operator()(_Args&& ...__args)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:1758:9: note: candidate template ignored: substitution failure [with _Args = <>]: implicit instantiation of undefined template
'std::__1::__bind_return<const <lambda at test3.cpp:108:17>, const std::__1::tuple<Dog>, std::__1::tuple<>, false>'
operator()(_Args&& ...__args) const
^
1 error generated.
내 질문
은 다음과 같습니다 :bind_fun()
호출 할 수없는 이유는 무엇
이
- 가 (컴파일되지 않습니다) 람다가를 rvalue 참조를 취하면
bind_fun()
, 주석 다음 컴파일 타임 오류가 남아? - 여기에서 람다에 대한 참조로 참조 및 값 참조를 사용하는 것의 차이점은 무엇입니까?
'std :: bind'는 바인딩 된 인수를 lvalues로 전달하므로 rvalue 참조와 일치하지 않습니다. –