This is indeed a good question; coming from an ASM/C background I am by no means a C++ expert and there may indeed be an easier way to achieve my underlying objective.
Having developed my own string class as part of the obligatory C++ initiation process, I am looking for an alternative yet convenient method of supporting generic C style string literals.
For example, assuming a bog-standard reference-counted class implementation, the following code structure would certainly be representative of "expected" usage:
1 2 3
|
BogStandardClass mystring1("Hello");
....
BogStandardClass mystring2(mystring1 + " and goodbye!");
|
However, in the vain hope of avoiding both buffer copy and strlen overhead, I was attempting to generate a preprocessor macro which would convert the statement _mymacro("Hello") into the appropriate length/string pair during compilation. Providing the pair is generated as a contiguous compound-POD as per the C implementation, I am able to directly reference the pre-compiled literal from within the executable' s const-segment and thus avoid unnecessary strlen and copy operations during object initialisation.
Thus, assuming the presence of a functional macro, the above code sequence could be rewritten as follows:
1 2 3
|
BogStandardClass mystring1(_mymacro("Hello"));
....
BogStandardClass mystring2(mystring1 + _mymacro(" and goodbye!"));
|
When all is said and done, what I'm effectively trying to do is coerce the way in which C/C++ compilers generate string literals within the executable's const-segment; from ASCIIZ to a custom length-prefixed format. However, considering the various strlen compile-time optimisations in conjunction with "standard" string usage, this entire question is more likely to procure academic interest than any significant performance optimisations.