{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreic3tjvujvop4jr4pabd72u6mcdcligcmolvxay7mvmqmw7jouin2i",
    "uri": "at://did:plc:vyjlfm46mfv6u4vjp6qtrfx2/app.bsky.feed.post/3mjfhy4j27n62"
  },
  "coverImage": {
    "$type": "blob",
    "ref": {
      "$link": "bafkreidg2k33tiuupnstmiowh7hyyvtsbe3gyhfygiytrzynysnhoctrxi"
    },
    "mimeType": "image/jpeg",
    "size": 81260
  },
  "path": "/articles/non-cogito-ergo-c-str",
  "publishedAt": "2026-04-13T06:30:00.000Z",
  "site": "https://thedailywtf.com",
  "tags": [
    "CodeSOD",
    "previously",
    "Learn more."
  ],
  "textContent": "**Tim** (previously) supports a relatively ancient C++ application. And that creates some interesting conundrums, as the way you wrote C++ in 2003 is not the way you would write it even a few years later. The standard matured quickly.\n\nWay back in 2003, it was still common to use C-style strings, instead of the C++ `std::string` type. It seems silly, but people had Strong Opinions™ about using standard library types, and much of your C++ code was probably interacting with C libraries, so yeah, C-strings stuck around for a long time.\n\nFor Tim's company, however, the migration away from C-strings was in 2007.\n\nSo they wrote this:\n\n\n    if( ! strncmp( pdf->symTabName().c_str(), prefix.c_str(), strlen( prefix.c_str() ) ) ) {\n        // do stuff\n    }\n\n\nThis is doing a \"starts with\" check. `strncmp`, `strlen` are both functions which operate on C-strings. So we compare the `symTabName` against the `prefix`, but only look at as many characters as are in the prefix. As is common, `strncmp` returns 0 if the two strings are equal, so we negate that to say \"if the `symTabName` starts with `prefix`, do stuff\".\n\nIn C code, this is very much how you would do this, though you might contemplate turning it into a function. Though maybe not.\n\nIn C++, in 2007, you _do not_ have a built-in `starts_with` function- you have to wait until the C++20 standard for that- but you have some string handling functions which could make this more clear. As Tim points out, the \"correct\" answer is: `if(pdf->symTabName().find(prefix) != 0UL)`. It's more readable, it doesn't involve poking around with `char*`s, and also isn't spamming that extra whitespace between every parenthesis and operator.\n\nTim writes: \"String handling in C++ is pretty terrible, but it doesn't have to be this terrible.\"\n\n[Advertisement] ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.",
  "title": "CodeSOD: Non-cogito Ergo c_str"
}