fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <utility>
  4.  
  5. /*Вопросы
  6. 1. Почему нельзя передать rvalue дальше?
  7. 2. Если обернуть в std::move, то заработает, правильно ли оборачивать в std::move?
  8. 3. Сейчас передачу rvalue's делаю как 'const Type& varname', это же правильный способ?
  9. */
  10. struct AA {
  11. std::string path;
  12. AA(const std::string&& path) {
  13. this->path = path;
  14. }
  15. };
  16. struct BB : public AA{
  17. //но если обертнуть в std::move, то все заработает
  18. BB(const std::string&& path) : AA(path){//std::move(path)
  19. }
  20. };
  21.  
  22. int main() {
  23. AA aa("Hello, simple rvalue!");//OK
  24. BB bb("Hello, complex rvalue!");
  25. std::cout << bb.path << std::endl;
  26. std::cout << static_cast<AA>(bb).path << std::endl;
  27.  
  28. return 0;
  29. }
Compilation error #stdin compilation error #stdout 0s 4956KB
stdin
Standard input is empty
compilation info
prog.cpp: In constructor ‘BB::BB(const string&&)’:
prog.cpp:18:36: error: cannot bind rvalue reference of type ‘const string&&’ {aka ‘const std::__cxx11::basic_string<char>&&’} to lvalue of type ‘const string’ {aka ‘const std::__cxx11::basic_string<char>’}
  BB(const std::string&& path) : AA(path){//std::move(path)
                                    ^~~~
prog.cpp:12:2: note:   initializing argument 1 of ‘AA::AA(const string&&)’
  AA(const std::string&& path) {
  ^~
stdout
Standard output is empty