2

I'm playing with an idea of an immutable string class in C++11.

One of the features I would like to have would be an ability to work with borrowed memory, where the user would promise during initialization that the buffer containing data will not change and will live longer than the string object. This type of initialization would probably be quite rare in practice so it would mainly be accessible through a static method.

For convenience however I would like constructions like ImmutableString("abc") to end up using the borrowed memory, since with string literals I can actually be sure about lifetime and constness of the value.

Is there a way to determine if a parameter is a string literal? An overload to const char[] is not enough since it can be automatically cast from plain char[] and the lifetime is not guaranteed in any way.

cube
  • 3,867
  • 7
  • 32
  • 52
  • 2
    Is this a possible duplicate of: [Verify type of string (e.g. literal, array, pointer) passed to a function](http://stackoverflow.com/q/5819217/514235) or [Restrict passed parameter to a string literal](http://stackoverflow.com/q/7613528/514235). If not then, I would recommend you to give some dummy code example to let other know what's the exact requirement? – iammilind Aug 04 '16 at 08:41
  • A workaround would be to make a copy of the string in debug mode and assert it has not changed every time you access the string. – Viktor Sehr Aug 04 '16 at 08:43
  • 1
    You could see if the pointer to the string resides in your .data/.rodata section as opposed to heap/stack. This should be a simple (ptr > sectionStart && ptr < sectionStart+sectionLength) kind of test. – PaulHK Aug 04 '16 at 08:47
  • 2
    `#define ImmutableString(X) "" X ""` will compile if the `X` is a string literal and would result in compiler error otherwise. This will happen during Preprocessing stage. The empty string literals will be optimized. This will guard you against the accidental usage of passing variables instead of string literals, However for situation such as `ImmutableString(+5), it may not help. – iammilind Aug 04 '16 at 08:53
  • @iammilind `ImmutableString(-);` – juanchopanza Aug 04 '16 at 08:55
  • @iammilind It's about 80% duplicate of these two :-) – cube Aug 04 '16 at 08:57
  • The macro approach just doesn't seem robust enough. Also the macro would need to have a different name and then I'd be better off with something like `ImmutableString::borrowed("abc")`. – cube Aug 04 '16 at 09:00
  • If you focus on existing free software C++ compilers, you could make some hack, e.g. have some [GCC](http://gcc.gnu.org/) plugin providing some `__builtin_is_literal_string` additional builtin. – Basile Starynkevitch Aug 04 '16 at 09:01
  • The 20% of not duplicate is that for that question it would be acceptable to make the ctor private and provide 2 static pseudo ctor methods, one borrowing the original memory the other copying it, and let the programmer choose. More or less saying *I cannot guess whether the string a litteral, but you, programmer, could know*. Unfortunately this question is closed, and this would not be an acceptable answer for the duplicate. – Serge Ballesta Aug 04 '16 at 09:44

0 Answers0