Date: Sun, 1 Feb 2026 23:46:04 +0000
On Sun, Feb 1, 2026 at 7:40 PM Sebastian Wittmeier wrote:
>
> So the actual string cannot be used as long as the substr is used?
>
> That is dangerous and unexpected.
I worked with a guy one time who was really melodramatic and used the
word 'dangerous' a lot when talking about C++ code.
If we're working on programming the microcontroller inside a
life-support machine ventilator, or the detonator for a missile, or
the thingie that keeps oxygen inside a submarine, then yeah sure we
can use the word 'danger' as there's the actual risk of life and limb.
But in this context where we're playing with strings, I'm downgrading
your 'dangerous' to 'a little worrisome'.
The following GodBolt shows what I had in mind:
https://godbolt.org/z/b34Y4j3xs
And here it is copy-pasted:
#include <cstddef> // size_t
#include <cstdio> // puts
#include <cstring> // strlen
#include <algorithm> // min
#include <stdexcept> // out_of_range
class TermString final {
using size_t = std::size_t;
char *const p;
char const saved = '\0';
TermString(char *const arg_p, size_t const len) noexcept
: p(arg_p), saved( arg_p[len] )
{
p[len] = '\0';
}
public:
~TermString(void) noexcept { p[std::strlen(p)] = saved; }
TermString(char *const arg_p) noexcept : p(arg_p) {}
operator char const*(void) const noexcept { return p; }
TermString substr(size_t const pos = 0, size_t const count = -1)
noexcept(false)
{
size_t const len = std::strlen(p);
if ( pos > len ) throw std::out_of_range("out_of_range");
size_t const index_of_null = std::min(count, len - pos);
return TermString(p + pos, index_of_null);
}
};
int main(void)
{
char buf[] = "\\\\.\\COM1\\AES128";
TermString ts(buf);
using std::puts;
puts(ts);
puts(ts.substr(4));
puts(ts);
puts(ts.substr(4,4));
puts(ts);
}
>
> So the actual string cannot be used as long as the substr is used?
>
> That is dangerous and unexpected.
I worked with a guy one time who was really melodramatic and used the
word 'dangerous' a lot when talking about C++ code.
If we're working on programming the microcontroller inside a
life-support machine ventilator, or the detonator for a missile, or
the thingie that keeps oxygen inside a submarine, then yeah sure we
can use the word 'danger' as there's the actual risk of life and limb.
But in this context where we're playing with strings, I'm downgrading
your 'dangerous' to 'a little worrisome'.
The following GodBolt shows what I had in mind:
https://godbolt.org/z/b34Y4j3xs
And here it is copy-pasted:
#include <cstddef> // size_t
#include <cstdio> // puts
#include <cstring> // strlen
#include <algorithm> // min
#include <stdexcept> // out_of_range
class TermString final {
using size_t = std::size_t;
char *const p;
char const saved = '\0';
TermString(char *const arg_p, size_t const len) noexcept
: p(arg_p), saved( arg_p[len] )
{
p[len] = '\0';
}
public:
~TermString(void) noexcept { p[std::strlen(p)] = saved; }
TermString(char *const arg_p) noexcept : p(arg_p) {}
operator char const*(void) const noexcept { return p; }
TermString substr(size_t const pos = 0, size_t const count = -1)
noexcept(false)
{
size_t const len = std::strlen(p);
if ( pos > len ) throw std::out_of_range("out_of_range");
size_t const index_of_null = std::min(count, len - pos);
return TermString(p + pos, index_of_null);
}
};
int main(void)
{
char buf[] = "\\\\.\\COM1\\AES128";
TermString ts(buf);
using std::puts;
puts(ts);
puts(ts.substr(4));
puts(ts);
puts(ts.substr(4,4));
puts(ts);
}
Received on 2026-02-01 23:44:59
