-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Open
Labels
Description
#include <string>
void f(std::string str)
{
const char *ptr = str.c_str();
std::string copy(ptr, 0, str.size()/2);
}
On Compiler Explorer: https://compiler-explorer.com/z/KEEPh3Kz1
The result:
[<source>:7:19: warning: constructor creating an empty string [bugprone-string-constructor]]
7 | std::string copy(ptr, 0, str.size()/2);
| ^
1 warning generated.
In this example, the following constructor of std::string should be called:
template< class StringViewLike >
basic_string( const StringViewLike& t,
size_type pos, size_type count,
const Allocator& alloc = Allocator() );
Since we provide a valid pointer and a valid non-0 count, the string isn't empty by default.
As such, the warning should not be given.