Issue 181540
Summary libc++ string constructor for repeating char fails using {} style
Labels libc++
Assignees
Reporter sambler
    Since c++11 we can call constructors using a `string str{"string"}` syntax.

There is a string constructor available as `(size_t n, char c)` that creates a string of c repeated n times. This constructor fails when using the `{}` format.

Some string constructors that work using `{}`
```
    string str2{"this is a string"};
    cout << str2 << endl;
    string str3{str2, 0, 4};
    cout << str3 << endl;
```

Using the repeated char constructor
```
    // works
    string str10(10, 'x');
    cout << str10 << endl;
    // fails
    string str11{10, 'x'}; // this results in a newline and one x
    cout << str11 << endl;
```

I am on FreeBSD 15-stable and tested with clang 19.1.7 and 21.1.8 with `-std=c++xx` from c++11 to c++26 and all get the same result.

Complete test sample
```
#include <iostream>
#include <string>

using namespace std;

int main()
{
    // works
    string str1("this is a string");
 cout << str1 << endl;
    string str2{"this is a string"};
    cout << str2 << endl;
    string str3{str2, 0, 4};
    cout << str3 << endl;

 // works
    string str10(10, 'x');
    cout << str10 << endl;
    // fails
    string str11{10, 'x'}; // this results in a newline and one x
 cout << str11 << endl;
}
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to