Creating Efficient Constant String class literals in C++

I am trying to figure out a way to create compile-time string literals for my custom string class that could be passed in place of "const String &" parameters, such that this parameter passing (1) would not create temporary string copies and (2) would not require counting string characters at runtime. The goal is to achieve the following:

void fn_taking_string_literal(const GString &str) { ... }

//Called as:
fn_taking_string_literal(GSTR("literal text"));

... where GSTR() is some kind of a macro, which generates a fake GString efficiently, without actually counting or copying characters.

My application was using such a technique with Visual Studio 6, however it stopped working once I ported to 7 (.NET), since studio 7 now puts string literals in CONST segment, crashing if they are ever modified. I am looking for a technique with similar syntax that can be implemented under studio 7. [Note that if anyone knows of a compiler option I can use to avoid strings being read-only, it would also help].

Below are the necessary details of my string class implementation, and my original GSTR macro.

The string class is implemented as simply a pointer to an allocated GStringHeader, which stored a length integer followed by string data:

struct GStringHeader
  UInt      Length;
  char      Data[4]; // Data + trailing zeros
                    // More is allocated as necessary for this array

class GString
  GStringHeader      *pData;      

    { pData = &GStringNullHeader; }

  // Constructor, does string copy
  GString(const GString &str);
  GString(const char *);

// String dummy object used for literal conversion
class GConstString
  union {
    char      *pData;
    UInt      *pSize;

  GConstString(char *p, UInt size)
    { pData = p; *pSize = size;  }

  operator const GString * ()
    { return ((const GString*) this); }

#define GSTR(str) *GConstString("    " str##"\0\0\0", sizeof(str)-1)

With this implementation, I could indeed write code such as:

fn_taking_string_literal(GSTR("literal text"));

Furthermore, the implementation was efficient – the compiler generated string literal was reinterpreted directly as allocated string class data. The only inefficiency was that the size was always stored in front of string data, but that was still much more efficient then copying all the characters and/or counting the length.

With visual studio 7 this no longer works, since any access such as

  char *ptext = "text";
  ptext[0] = 'c';

causes an access violation.

I tried switching a GConstString constructor to

GConstString(char p[], UInt size)
  { pData = p; *pSize = size;  }

and that, surprizingly, did not solve the problem.

Does anyone have any suggestions?
Who is Participating?
moduloConnect With a Mentor Commented:
PAQed with points refunded (400)

Community Support Moderator
CString   and std::string are all efficient when you don't need to write to it, for they are COW (copy on write).

if you still doubt of performance of COW, you can pass referrence to string/CString instead of creating another class.

Or use BSTR, but you have to convert constant string to BSTR manually.

welcome to
mikeant78Author Commented:
I understand the implications of COW, and implementation details of various existing string classes. No matter what implementation is, it would at the least need to compute the string length on the initial string construction (that is, if it chooses to store the length in addition to the trailing zero, as it should).

My question instead focuses on how to reinterpret a C++ string literal, i.e. a quoted string, as string class of choice. The goal is to optimize/elimiate the initial construction of a string, not optimize its passing mechanism once it has been constructed.

I would like to have a macro, or some kind of inline mechanism that would allow me to statically initalize {string_length, string_data} tuple that can then be converted/cast into a string class.
mikeant78Author Commented:

I have actually figured out the solution which works... If i put in pragmas

#pragma data_seg(".rwdata")
#pragma const_seg(".rwdata")

in my original code (discussed above) it works correctly in the Visual Studio 7, because these pragmas cause strings to go into a writable segment instead of read-only segment.

Since I did not receive any real assistance in this question, I would like my points refunded, if possible.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.