c++c++20user-defined-literals

A problem with user-defined literal operator (in raw mode)


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'

Solution

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