To answer the question, as asked, no it is not possible.
As an example of the impossibility, assume we have a path specified as "C:\a\b"
;
Now, str
is actually represented in memory (in your program when running) using a statically allocated array of five characters with values {'C', ':', '\007', '\010', '\000'}
where '\xyz'
represents an OCTAL representation (so '\010'
is a char
equal to numerically to 8
in decimal).
The problem is that there is more than one way to produce that array of five characters using a string literal.
char str[] = "C:\a\b";
char str1[] = "C:\007\010";
char str2[] = "C:\a\010";
char str3[] = "C:\007\b";
char str4[] = "C:\x07\x08"; // \xmn uses hex coding
In the above, str1
, str2
, str3,
and str4
are all initialised using equivalent arrays of five char
.
That means convert_to_raw("C:\a\b")
could quite legitimately assume it is passed ANY of the strings above AND
std::cout << convert_to_raw("C:\a\b") << '\n';
could quite legitimately produce output of
C:\007\010
(or any one of a number of other strings).
The practical problem with this, if you are working with windows paths, is that c:\a\b
, C:\007\010
, C:\a\010
, C:\007\b
, and C:\x07\x08
are all valid filenames under windows - that (unless they are hard links or junctions) name DIFFERENT files.
In the end, if you want to have string literals in your code representing filenames or paths, then use \\
or a raw string literal when you need a single backslash. Alternatively, write your paths as string literals in your code using all forward slashes (e.g. "C:/a/b"
) since windows API functions accept those too.