Remix.run Logo
▲ coldpie 6 hours ago

Curious if you can elaborate. I found learning C to be a really good stepping stone to understanding many of C++'s design choices (eg stuff like why the 'virtual' keyword exists & works as it does; or why a class's private members must be declared in the public header). It also gets you more familiar with non-interpreted language concepts like the compiler and the linker before C++ turns those into multi-headed beasts.

▲0x457 an hour ago | parent | next [-]

I'd distinguish between understanding why C++ was designed the way it was and learning how to write idiomatic C++. C is certainly useful for the former, but many practices considered perfectly normal in C are considered (very) bad practice in C++.

This is also why I dislike the term "C/C++" when referring to them as if they were a single language. They are two distinct languages with substantially different idioms.

What I hate about C++ is that it offers a myriad of ways to accomplish the same thing, ranging from outright terrible to genuinely good. Learning C first can reinforce habits that you'll later have to unlearn.

▲pjmlp 5 hours ago | parent | prev [-]

C raw arrays and strings should only be used for "unsafe" C++ code, in the implementation of safer types, with bounded checked interfaces, or in interop code with C libraries, properly wrapped in safe accessors for example.

Use of references for any non nullable pointer.

RAII for heap allocated types, eventually new/delete, leave malloc()/free() family with sizeof type math for interop with existing C code.

Prefer standard library algorithms with proper bounds checking, instead of memsomething and strsomething function family.

Preprocessor magic, when templates and constexpr are type safe, and can even be single stepped on a modern IDE.

Just some examples out of my mind.

▲PhilipRoman 4 hours ago | parent [-]

I wholeheartedly disagree with the idea that all C++'s problems come from backwards compatibility with C. Even "pure" C++ alone without C constructs brings enough footguns.

  struct Config {
    std::vector<std::string> names;
    const std::vector<std::string>& getNames() const { return names; }
  };
  Config loadConfig();
  
  // which of these are safe?
  for (const auto& n : loadConfig().names)      { use(n); }  // A
  for (const auto& n : loadConfig().getNames()) { use(n); }  // B
  
  const auto& v1 = loadConfig().names;          // C
  for (const auto& n : v1) { use(n); }
  
  const auto& v2 = loadConfig().getNames();     // D
  for (const auto& n : v2) { use(n); }
Trick question btw
▲plorkyeran 2 hours ago | parent | next [-]

C in C++20, A, B and C in C++23. This does sort of fall under a backwards compatibility issue, albeit not with C, as making D valid was considered too much of a breaking change.

▲PhilipRoman 21 minutes ago | parent [-]

I'm no C++ expert, but in P2012R0 it even gives this specific example, indicating that A is OK in c++20:

[..] One recent real-world example by one of the authors is the following: Improving the following code:

  struct Person {
   std::vector<int> values;
   …
  };
  for (auto elem : createPerson().values) { // OK (lifetime extended) <-----------
by introducing a getter to have better encapsulation, suddenly caused the undefined behavior:

  class Person {
   private:
   std::vector<int> values;
   …
   public:
   const auto& getValues() const {
   return values;
   }
  };
  for (auto elem : createPerson().getValues()) { // UB (fatal runtime error)
▲trealira 4 hours ago | parent | prev [-]

Why not simply write the following?

   const Config config { loadConfig() };
   for (auto &name : config.names) {
       use(name);
   }
Or else you could have "loadConfig()" be a method that returns a reference, which is in turn the member of a class, so that there isn't any dangling reference issue.