Hot answers tagged

189 votes
Accepted

Should I stop using the term C/C++?

C was never a subset of C++. The most obvious example of this is int new;. This has been true since C89 and C++98, and the languages have only grown further from each other as new standards have come ...
xhainingx's user avatar
  • 1,432
75 votes
Accepted

Is passing arguments as const references premature optimization?

"Premature optimisation" is not about using optimisations early. It is about optimising before the problem is understood, before the runtime is understood, and often making code less readable and less ...
gnasher729's user avatar
  • 43.6k
44 votes

Should I stop using the term C/C++?

Going against the flow I would say it depends on the context. The term "C/C++" is usually not appropriate when saying something like "this is a C/C++ program", but this has been explored to depth in ...
vsz's user avatar
  • 1,486
31 votes

Should I stop using the term C/C++?

In general the SO users ask the person who is asking the question to choose a language: C or C++. Why? There are many subtle differences between C and C++. For example, in C++, a const variable at ...
Brian's user avatar
  • 617
28 votes
Accepted

Is it ever bad to mark a C++ function constexpr?

This matters only if the function is part of a public interface, and you want to keep future versions of your API binary-compatible. In that case, you have to think carefully how you want to evolve ...
amon's user avatar
  • 134k
25 votes

Style guide for C++

The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++, you can find them here: https://github.com/isocpp/CppCoreGuidelines They are written by, ...
Alessandro Jacopson's user avatar
21 votes
Accepted

Object lifetime invariants vs. move semantics

It's usually bad form in C++ to create zombie objects that explode if you touch them. But that's not what you're doing. You're creating a "zombie object" that will explode if you touch it ...
Nicol Bolas's user avatar
  • 11.9k
21 votes

Is passing arguments as const references premature optimization?

TL;DR: Pass by const reference is still a good idea in C++, all things considered. Not a premature optimization. TL;DR2: Most adages don't make sense, until they do. Aim This answer just tries to ...
rwong's user avatar
  • 16.7k
20 votes
Accepted

Are all the objects in C++ mutable if not stated otherwise?

Immutability has been well-understood for some time. Python, Java, and C++ have different memory models that make direct comparisons difficult. The author of the article you originally cited (...
amon's user avatar
  • 134k
17 votes

Should I stop using the term C/C++?

I was always told that C is a subset of C++ or C++ is C with classes. And that was quiet true until the appearance of C++x0, C++11 (or the modern C++ 11/14/17 in general). C has never been a subset ...
ouah's user avatar
  • 287
17 votes

Should I stop using the term C/C++?

Some programs are written in a mixture of C and C++ This is just a fact of life. You can compile object files from C and C++ and link them together. The result can quite reasonably be called "a C/...
Ben's user avatar
  • 853
16 votes

Is it ever bad to mark a C++ function constexpr?

Marking a function as constexpr also makes it an inline function §[dcl.constexpr]/1: A function or static data member declared with the constexpr specifier is implicitly an inline function or ...
Jerry Coffin's user avatar
  • 44.5k
15 votes
Accepted

"The C++ Programming Language" confusion on Comparison

It's a little hard to be certain when you take such a short quote out of context, but I'd assume that he's pointing to the fact that comparisons of simple objects normally don't involve any function ...
Jerry Coffin's user avatar
  • 44.5k
14 votes

Should I stop using the term C/C++?

Should I stop using the term C/C++? Absolutely. It is not clear what this construct is intended to express except, perhaps, confusion about what C and C++ are on behalf of the person who uses the ...
5gon12eder's user avatar
  • 6,976
14 votes
Accepted

Is there any benefit to to define constant local variables as static ( c++)?

Beyond @Christophe's very good answer, the code generated for the static is most likely worse than the one for the local variable, so if you're interested the under-the-hood benefit, statics are worse ...
Erik Eidt's user avatar
  • 33.6k
12 votes
Accepted

Is it possible to achieve Rust's ownership model with a generic C++ wrapper?

C++ has three ways to pass parameters to a function: by value, by lvalue reference, and by rvalue reference. Of these, passing by value creates ownership in the sense that the called function receives ...
amon's user avatar
  • 134k
12 votes
Accepted

Why did C++11 add find_if() instead of overloading find()?

Let's take a look at two of the relevant functions: template <class InputIt, class T> InputIt find(InputIt first, InputIt last, const T& value); template <class InputIt, class ...
Deduplicator's user avatar
  • 8,701
11 votes

Favored syntax for calling a function with parameters that another function returns

In most situations, it's about which version is most readable, which is often in the eye of the beholder. In simple cases, composition is very clear, but past a certain point, verbosity adds clarity. ...
Karl Bielefeldt's user avatar
11 votes

Are all the objects in C++ mutable if not stated otherwise?

Almost. The language itself treats everything as mutable unless you use const, but it's still possible to build immutable objects without telling the compiler they're immutable, by simply not ...
user253751's user avatar
  • 4,863
11 votes
Accepted

'Assigning' a base class to a derived class?

It all depends on what A and B are and mean. Because base class subobjects can do things that member subobjects cannot (being empty not disturbing the layout of the type, automatically exposing their ...
Nicol Bolas's user avatar
  • 11.9k
11 votes

Is passing arguments as const references premature optimization?

In DonaldKnuth's paper "StructuredProgrammingWithGoToStatements", he wrote: "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts ...
Lawrence's user avatar
  • 637
10 votes

What are R-value references used for?

A common use case is the “move constructor,” invoked when an object is being copied from a temporary that’s about to expire (a rvalue). An example is foo = bar + baz; where bar + baz is a temporary ...
Davislor's user avatar
  • 1,523
10 votes

Will destructing a large list overflow my stack?

Late answer but since no one provided it... I ran into the same issue and solved it by using a custom destructor: virtual ~node() noexcept { while (next) { next = std::move(next->next); ...
Holt's user avatar
  • 201
10 votes

Is the meaning of `const` still thread-safe in C++11?

The video you cited is for an advanced and expert audience. Herb Sutter tries to bring this audience to a consensus on how to best communicate the intent of these keywords to other people, in this age ...
rwong's user avatar
  • 16.7k
9 votes
Accepted

What Design to choose for Parsing different files to populate different classes?

Classes should have a single responsibility. Presumably the responsibility of parsing a file to populate a class and the responsibilities of class itself are distinct and should not be combined. ...
Samuel's user avatar
  • 9,167
9 votes

What Design to choose for Parsing different files to populate different classes?

If the parsing is more complex than a few lines of code, it is probably better to put it into separate classes, lets call them Class1Parser, Class2Parser and so on. If those classes contain similar ...
Doc Brown's user avatar
  • 203k
9 votes

Is passing arguments as const references premature optimization?

Passing by ([const][rvalue]reference)|(value) should be about the intent and promises made by the interface. It has nothing to do with performance. Richy's Rule of Thumb: void foo(X x); // ...
Richard Hodges's user avatar
8 votes

Style guide for C++

The criticisms of Google's C++ style guide (and I agree some are justified) are not about Google's naming conventions or indentation style but rather about some of their other rules and policies. ...
mattnewport's user avatar
8 votes
Accepted

How to properly deal with starvation

The scheduling mechanism you have described is Fixed-priority pre-emptive scheduling. If you know there is a possibility the max priority queue is always full, then you are using the wrong mechanism, ...
UmNyobe's user avatar
  • 372

Only top scored, non community-wiki answers of a minimum length are eligible