Issue 76394
Summary User-defined literals does not work, even with -Wno-error=reserved-user-defined-literal enabled
Labels new issue
Assignees
Reporter tada123
    Trying to compile following code:
```cpp
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <cstdio>

class MyString {
public:
    size_t sz;
    char* str;
    
    inline ~MyString(){
        free(this->str);
    }
    inline char* c_str(){
        return str;
    }
};

MyString operator "" M(const char* str, size_t sz){
    return MyString {sz, strdup(str)};
}

MyString test(){
    MyString s = "Qqqq"M;
    return s;
}
int main(int argc, char** argv){
    puts(test().c_str());
}
```
Compiling with only `g++` works fine with this output:
```
clang_literal.cpp:20:10: warning: literal operator suffixes not preceded by ‘_’ are reserved for future standardization [-Wliteral-suffix]
   20 | MyString operator "" M(const char* str, size_t sz){
      |          ^~~~~~~~
```
However, compilation with `clang++` gives error messages even with `-Wno-error=reserved-user-defined-literal enabled` enabled:

```
 >>>  clang++ -Wno-error=reserved-user-defined-literal -Wno-reserved-user-defined-literal clang_literal.cpp
clang_literal.cpp:20:10: warning: user-defined literal suffixes not starting with '_' are reserved; no literal will invoke this operator [-Wuser-defined-literals]
MyString operator "" M(const char* str, size_t sz){
         ^
clang_literal.cpp:25:14: error: no viable conversion from 'const char[5]' to 'MyString'
    MyString s = "Qqqq"M;
             ^   ~~~~~~
clang_literal.cpp:7:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char[5]' to 'const MyString &' for 1st argument
class MyString {
      ^
clang_literal.cpp:25:24: error: expected ';' at end of declaration
    MyString s = "Qqqq"M;
                       ^
                       ;
1 warning and 2 errors generated.
```
Yes, i know, that adding underscore fixes the problem, but i have really huge project, which does not use underscores for custom literals.
Is it a bug, or is there some another flag/workaround (except `-std=c++9`), that must be enabled to allow the code to compile with `clang++` same way as `g++` does?
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to