Skip to main content
77 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
  • 49.4k
22 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
  • 17.2k
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.8k
12 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
  • 657
12 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
  • 12.1k
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
  • 136k
12 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
  • 17.2k
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
  • 9,309
11 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
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
  • 220k
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,247
8 votes
Accepted

Implementing reference counting from scratch or using shared_ptr for resource?

unique_ptr<T, D> is actually specially designed to be able to work with more arbitrary handle-like types. I spelled out the template name fully because D is the key here. Normally unique_ptr<...
Nicol Bolas's user avatar
  • 12.1k
8 votes

Introduce code standard into old code

This is where encapsulation pays off. If you have functionality that's encapsulated in a method or class, that's written in the old MFC style, and you instantiate or call it using new-style code, it ...
Robert Harvey's user avatar
8 votes
Accepted

Is TC++PL 4th Edition by Bjarne Stroustrup outdated?

Any programming book is practically guaranteed to be outdated as soon as it hits the shelves. The Stroustrup book is no exception. But that does not mean such books would be useless. C++11 introduced ...
amon's user avatar
  • 136k
7 votes
Accepted

Should repeatedly-computed quantities derived from member data be stored in member data containers?

The simplest code is generally the best, and repeatedly computing the result using code that mutates no memory is generally the simplest. Memoization is a technique you can use to increase performance ...
Frank Hileman's user avatar
7 votes
Accepted

Using vectors of shared pointers to objects in my C++ code to prevent object duplication

The right solution will depend a lot on your non-functional requirements. How many particles will you typically have? How many contacts does a particle typically have? How much is a network updated, ...
Chris Drew's user avatar
7 votes

Handling errors for non-exceptional cases in modern C++

Boost::variant and Boost::optional would be the libraries I recommend looking at. They are tremendously effective at minimizing the extra costs of construction because they wont initialize a member ...
Cort Ammon's user avatar
  • 11.9k
7 votes

Combining the arguments of a function using a single structure

OK, so what you're talking about is a function of the form: struct func_params { int param1; float param2; ... }; void func(func_params params) {...} I will assume that the hypothetical ...
Nicol Bolas's user avatar
  • 12.1k
7 votes

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

Is it good practice while designing a thread-safe application to mark every member function const and every member variable mutable? No, that is not good practice. A const member function signals ...
Bart van Ingen Schenau's user avatar
6 votes
Accepted

Does an explicit temporary of an integral type qualify as an integral constant expression?

Yes, an explicit temporary such as int() can be an integral constant expression (provided, of course, that it is actually constant). This is a red herring. Your versions (and flags) of Clang and GCC ...
Quentin's user avatar
  • 1,475
6 votes
Accepted

How to pass a mock as std::unique_ptr to Class under test

The easiest way would be to keep class B exactly the same as it was before, but grab the pointer from ‘ma’ before you pass it to ‘b’ in the test case. Like so… TEST(...){ auto ma = std::...
Ryan's user avatar
  • 613
6 votes
Accepted

Refactoring large code base legacy C++ project that mixed with C style

C code using the goto cleanup style like this: int function(int argument) { int result; opaque_handle handle = NULL; char* text = NULL; size_t size; handle = extlib_create(); if (!handle)...
Sebastian Redl's user avatar
6 votes

C++ memory visibility, passing data between threads

This is a question on cache coherency. Modern CPUs organize the memory into "cache lines"; they track the current status of cache lines across cores and levels of caches. Examples of cache ...
rwong's user avatar
  • 17.2k
5 votes

How to pass a mock as std::unique_ptr to Class under test

To begin with, Ryan's answer is justifiable, because the mock is being used in a white-box testing environment, which means the programmer can see all source code, and therefore knows exactly how to ...
rwong's user avatar
  • 17.2k
5 votes
Accepted

Modular Design affects compilation time

Kind of. The tricky part is the role of header files. The C/C++ compilation model handles one "compilation unit" at a time. Roughly, there is one compilation unit per .cpp file. Different ...
amon's user avatar
  • 136k
4 votes
Accepted

Function returning different tuples

The short answer is that no it is not. The compiler can do some type deduction to figure out the return type without your specifying it explicitly (at least in some cases), but in every case the type ...
Jerry Coffin's user avatar
  • 44.8k
4 votes

Who is to blame for this range based for over a reference to temporary?

2025 answer: it is not a bug anymore since C++23, thanks to P2718R0's change to wording of temporary extension in range-based for-loops: There are four contexts in which temporaries are destroyed at ...
Jean-Michaël Celerier's user avatar
4 votes
Accepted

What is the best object-oriented design approach for a tree with two node types?

Another option, besides the three you mention, would be to separate the data to its own class and then have the basic node interface provide both that and children in an optional way. For the ...
Errorsatz's user avatar
  • 776
4 votes
Accepted

How does a blocking call work?

A blocking call works however it is that the OS and implementation want them to work. doesn't there have to be a busy wait somewhere in order to determine when the wait condition is fulfilled? That ...
Nicol Bolas's user avatar
  • 12.1k
3 votes

Using vectors of shared pointers to objects in my C++ code to prevent object duplication

So, this really depends on the access patterns you anticipate. First, shared_ptr comes at a potentially large cost in a multi-threaded program. Incrementing and decrementing the reference counts will ...
Omnifarious's user avatar

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