Skip to content

Code analysis warning string_concat.hpp C26800: Use of a moved from object #3805

@igirock

Description

@igirock

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).

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:

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

Metadata

Metadata

Assignees

Labels

kind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmation

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions