0

I am trying to define a literal operator in literal mode (i.e) the function parameter list should be const char* arg1 only not const char* arg1,size_t size but I can't

#include<iostream>
#include<string>




int operator"" _i(const char* charsArr){

        return std::stoi(std::string{charsArr});
    }


int main(){
        std::cout<<"1234324"_i;

        return 0;
    }

The compiler error message

error C3688: invalid literal suffix '_i'; literal operator or literal operator template 'operator ""_i' not found
note: Literal operator must have a parameter list of the form 'const char *, std::size_t'
asmmo
  • 6,922
  • 1
  • 11
  • 25

1 Answers1

3

UDLs as applied to string literals only have one mode (2 in C++20, but the new one is not applicable to your case): you get a pointer and a size. Only non-string UDLs have a single-argument const char* form.

That is, string literals in C++ can contain embedded NUL characters, and your UDLs aren't allowed to pretend otherwise.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • So, If on deleting the two quotes in `std::cout<<"1234324"_i;`, the code compiles and 1234324 will be recieved as const `char*` (c style array) in the operator function?? – asmmo Jan 01 '20 at 20:48
  • @anonymous: Yes. But you wouldn't need the `_i` at all. What is the problem with getting a size for the string anyway? Just do it the right way. – Nicol Bolas Jan 01 '20 at 20:52
  • I just wanted to check something – asmmo Jan 01 '20 at 20:54