TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24352 17
An interesting trick: avoid dangling for strings

Hello everyone,

Yesterday I was writing a piece of code to interface with C, and I found an interesting thing I wanted to share. I would like feedback to know if it is correct as well, I am not 100% certain.

The context is: how can I make sure I only accept a string literal for a function to not endanger dangling?

Here it goes:


So I had this function:

void pthread_set_name_np(pthread_t thread, const char *name);


That I wanted to encapsulate to give my thread a name. The string for name is never copied, hence, it is easy you can make it dangle:

void setThreadName(const char * name, std::optional<std::reference_wrapper<std::thread>> thread = std::nullopt) {
auto nativeHandle = thread.has_value() ? thread.value().get().native_handle() : pthread_self();
pthread_setname_np(nativeHandle, name);
}


If name buffer provenance is passed as a temporary string and goes out of scope, then it would dangle:

...
{
std::string threadName = "TheNameOfTheThread";
setThreadName(threadName.data());
}

// string is destroyed here, .data() points to dangling memory area.


So the question is:

How can I enforce a string literal is passed and nothing else?

I came up with this:


struct LiteralString {
char const* p;

template<class T, std::size_t N>
requires std::same_as<T, const char>
consteval LiteralString(T (&s)[N]) : p(s) {}
};


void setThreadName(LiteralString name, std::optional<std::reference_wrapper<std::thread>> thread = std::nullopt) {
auto nativeHandle = thread.has_value() ? thread.value().get().native_handle() : pthread_self();
pthread_setname_np(nativeHandle, name.p);
}

std::string threadName("threadName");

setThreadName(threadName.data()); // FAILS, const char * not compatible.

// Works, does not dangle, since a string literal is static and an lvalue
setThreadName(LiteralString("threadName"));


Any thoughts? Is this correct code? How would you make it more ergonomic, maybe with some conversion?












https://redd.it/1p6x4qr
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
More from @r_cpp
  1. Oct 12, 2026ACCU Overload Journal 195 - October 2026 https://accu.org/journals/overload/overload195 ht…
  2. Oct 12, 2026C++ Contracts C++ contracts are a good thing, why are people saying it's a bad thing? Anyb…
  3. Oct 11, 2026wxWidgets 3.2.12 and 3.3.4 Released https://wxwidgets.org/news/2026/10/wxwidgets-3.2.12-an…
  4. Oct 11, 2026VKRT NES Remaster — an open-source C++20 NES emulator with an observation-driven remasteri…
  5. Oct 11, 2026Right way to test async code https://redd.it/1x2wqgz @r_cpp
  6. Oct 10, 2026Yantra: an LALR(1) parser generator for C++23 Yantra: an LALR(1) parser generator for C++2…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →