-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Description
Description
A code analysis warning is reported for string_concat.hpp line 141.
##[warning]Include\nlohmann\detail\string_concat.hpp(141,0): Warning C26800: Use of a moved from object: ''(*<args_2>)'' (lifetime.1).
json/include/nlohmann/detail/string_concat.hpp
Lines 136 to 143 in a3e6e26
template<typename OutStringType = std::string, typename... Args> | |
inline OutStringType concat(Args && ... args) | |
{ | |
OutStringType str; | |
str.reserve(concat_length(std::forward<Args>(args)...)); | |
concat_into(str, std::forward<Args>(args)...); | |
return str; | |
} |
As far as I understand, the issue seems to be that std::forward is used two times here.
It could be possible that the concat_length function moves the "args", and then they would be empty for the call on line 141.
Possible solution:
json/include/nlohmann/detail/string_concat.hpp
Lines 30 to 31 in a3e6e26
template<typename StringType, typename... Args> | |
inline std::size_t concat_length(const StringType& str, Args&& ... rest); |
I think the concat_length function does not need a forwarding reference, as you never want to "move" there.
So i think changing it to something like this would fix it:
template<typename StringType, typename... Args>
inline std::size_t concat_length(const StringType& str, const Args& ... rest);
And then simply drop the std::forward on the call.
Reproduction steps
Do a Code Analysis build with MSVC 17.3 and use "Microsoft Native Recommended Rules"
Expected vs. actual results
No code analysis warning
Minimal code example
No response
Error messages
##[warning]Include\nlohmann\detail\string_concat.hpp(141,0): Warning C26800: Use of a moved from object: ''(*<args_2>)'' (lifetime.1).
Compiler and operating system
Win10; MSVC 17.3 (v143 platform toolset); c++ language standard: /std:c++latest
Library version
3.11.2 (include version)
Validation
- The bug also occurs if the latest version from the
develop
branch is used. - I can successfully compile and run the unit tests.