Conversion from string to const char* + size_t in ""operator

john s.

I am reading about the ""operator and found some weird looking code snip, I did not understand.

I did not understand the conervsion from string "110011" to const char *s, size_t l ?

I was expecting something like:

int operator "" _b (const std::string) { .. } or int operator "" _b (const char * s) { .. }

userliteral.cpp

...
int operator "" _b (const char * s, size_t l)
{
     int decimal {0};
     ...//conversion
     return decimal;
} 

main.cpp

int bin2dez01 = "110011"_b;    //<--- string "110011" to const char *s, size_t l ??????
std::cout << bin2dez01 << "\n";
Matthew Walton

operator "" converts a string literal within the program source into another type, in this case an int based on parsing a string representation of a binary number.

The type of a string literal in C++ is const char * - a constant pointer to an array of char representing the contents of the string. You also get a size_t which tells you how long the string is, because you can't tell that just from the pointer.

(The standard for strings in C and C++ is to put a null byte \0 to mark the end of the string, but finding that isn't free, and sometimes they get left out by mistake, so it's always easier to pass the pointer and the length around when you can).

The standard string class std::string can be constructed from a string literal, but unlike in languages like C# and Java they are absolutely not the same type. You get the illusion of it a lot because std::string has various conversion operators and overloads to allow you to work more or less seamlessly with string literals in the ways you'd expect to be able to.

So in summary, there's no conversion going on - the string literal really is a const char *, and the size_t tells you how long it is.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++: invalid conversion from 'const char*' to 'size_t'?

From Dev

error: cannot convert const string to const char* for argument 1 to size_t strlen(const char*)

From Dev

error: cannot convert const string to const char* for argument 1 to size_t strlen(const char*)

From Dev

No suitable conversion function from "std::string" to "const char *" exists

From Dev

Conversion from int to c-string (const char*) fails

From Dev

No conversion from "const char" to "int"

From Dev

No conversion from DWORD to const char*

From Dev

conversion from `const char*' to `byte'

From Dev

Conversion of const size_t in mex

From Dev

Invalid conversion from ‘const char*’ to ‘unsigned char*’

From Dev

invalid conversion from 'const char*' to 'char*'

From Dev

Invalid conversion from 'char' to 'const char *'

From Dev

error: invalid conversion from ‘char’ to ‘const char*

From Dev

invalid conversion from ‘const char*’ to ‘char*

From Dev

error: invalid conversion from ‘char’ to ‘const char*

From Dev

Conversion not valid from char to const char*

From Dev

No viable conversion from 'const std::__1::basic_string<char> to 'std::__1::basic_string<char> *'

From Dev

Invalid conversion from char to const char when using Insert function from <string>

From Dev

Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

From Dev

Why conversion from char* to std::string is more preferred than to const char* in the snippet?

From Dev

invalid conversion from ‘const char*’ to ‘char’ / uninitialized const member in struct

From Dev

conversion from 'size_t' to 'rapidjson::SizeType'

From Dev

conversion from 'size_t' to 'rapidjson::SizeType'

From Dev

String conversion from System::String ^ to const wchar_t *

From Dev

Casting from size_t to char and around

From Dev

Conversion from string literal to char *

From Dev

Invalid Conversion from char to string

From Dev

How to resolve this: invalid conversion from 'const char*' to 'const uint8_t*

From Dev

error: invalid conversion from 'void*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive]

Related Related

  1. 1

    C++: invalid conversion from 'const char*' to 'size_t'?

  2. 2

    error: cannot convert const string to const char* for argument 1 to size_t strlen(const char*)

  3. 3

    error: cannot convert const string to const char* for argument 1 to size_t strlen(const char*)

  4. 4

    No suitable conversion function from "std::string" to "const char *" exists

  5. 5

    Conversion from int to c-string (const char*) fails

  6. 6

    No conversion from "const char" to "int"

  7. 7

    No conversion from DWORD to const char*

  8. 8

    conversion from `const char*' to `byte'

  9. 9

    Conversion of const size_t in mex

  10. 10

    Invalid conversion from ‘const char*’ to ‘unsigned char*’

  11. 11

    invalid conversion from 'const char*' to 'char*'

  12. 12

    Invalid conversion from 'char' to 'const char *'

  13. 13

    error: invalid conversion from ‘char’ to ‘const char*

  14. 14

    invalid conversion from ‘const char*’ to ‘char*

  15. 15

    error: invalid conversion from ‘char’ to ‘const char*

  16. 16

    Conversion not valid from char to const char*

  17. 17

    No viable conversion from 'const std::__1::basic_string<char> to 'std::__1::basic_string<char> *'

  18. 18

    Invalid conversion from char to const char when using Insert function from <string>

  19. 19

    Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

  20. 20

    Why conversion from char* to std::string is more preferred than to const char* in the snippet?

  21. 21

    invalid conversion from ‘const char*’ to ‘char’ / uninitialized const member in struct

  22. 22

    conversion from 'size_t' to 'rapidjson::SizeType'

  23. 23

    conversion from 'size_t' to 'rapidjson::SizeType'

  24. 24

    String conversion from System::String ^ to const wchar_t *

  25. 25

    Casting from size_t to char and around

  26. 26

    Conversion from string literal to char *

  27. 27

    Invalid Conversion from char to string

  28. 28

    How to resolve this: invalid conversion from 'const char*' to 'const uint8_t*

  29. 29

    error: invalid conversion from 'void*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive]

HotTag

Archive