boostorg / dll

Library for comfortable work with DLL and DSO
https://boost.org/libs/dll
52 stars 43 forks source link

Wrong code in cpp_test_library.cpp: non-const lvalue reference to type 'some_space::some_class' cannot bind to a tempor ary of type 'some_space::some_class' #53

Closed yuxianch closed 3 years ago

yuxianch commented 3 years ago

https://github.com/boostorg/dll/blob/ac134827f348b33dcdc3814b42cff57c2e792aad/test/cpp_test_library.cpp#L93-L102

Compiling this file with "clang-cl -std:c++latest", it will get the following error(the error doesn't show up when using -std:c++20 or older):

+ clang-cl cpp_test_library.cpp -TP /Z7 /Od /Ob0 /W3 /GR /MDd /EHs -std:c++latest -D_CRT_USE_BUILTIN_OFFSETOF -c -DBOOST_ALL_NO_LIB=1 -DBOOST_FILESYSTEM_DYN_LINK=1 -DBOOST_SYSTEM_DYN_LINK=1 -DBOOST_SYSTEM_NO_DEPRECATED '-I..\..\..'
cpp_test_library.cpp(37,56): warning: volatile-qualified parameter type 'const volatile int' is deprecated [-Wdeprecated-volatile]
BOOST_SYMBOL_EXPORT void overloaded(const volatile int i)
                                                       ^
cpp_test_library.cpp(102,61): error: non-const lvalue reference to type 'some_space::some_class' cannot bind to a temporary of type 'some_space::some_class'
some_class& some_class::operator=(some_class &&ref) {return ref;}
                                                            ^~~
1 warning and 1 error generated.
+ clang-cl cpp_test_library.cpp -TP /Z7 /Od /Ob0 /W3 /GR /MDd /EHs -std:c++20 -D_CRT_USE_BUILTIN_OFFSETOF -c -DBOOST_ALL_NO_LIB=1 -DBOOST_FILESYSTEM_DYN_LINK=1 -DBOOST_SYSTEM_DYN_LINK=1 -DBOOST_SYSTEM_NO_DEPRECATED '-I..\..\..'
cpp_test_library.cpp(37,56): warning: volatile-qualified parameter type 'const volatile int' is deprecated [-Wdeprecated-volatile]
BOOST_SYMBOL_EXPORT void overloaded(const volatile int i)
                                                       ^
1 warning generated.

In line 102, ref is an rvalue reference (a temporary) and it's being returned as an lvalue reference that cannot have its lifetime extended. A workaround is to change the return type to be a const lvalue reference so the lifetime can be extended (or actually honor the move semantics of the parameter).

apolukhin commented 3 years ago

Many thanks for the issue report. Fixed it in a slightly different way, than was proposed in #55