103 questions
6
votes
2
answers
282
views
How do I follow "C.12 - Don’t make data members const or references in a copyable or movable type" while keeping track of some const value?
When I have some const members variables in my class, I would like to know how to respect core guideline C.12 to keep a default copy-assignment operator.
C.12: Don’t make data members const or ...
8
votes
2
answers
274
views
Why should I be careful in "noexcept-ing all the functions" just like I "const all the things"?
I haven't found a video about const all the things ¹, but there's at least Con.3: By default, pass pointers and references to consts from the Cpp Core Guidelines.
There's lots of presentations and ...
3
votes
1
answer
260
views
Using CMake, how can we check if ".h" files are "self-contained" (as per guidelines)?
There is the following C++ Core Guideline:
Header files should be self-contained
In large project, how can we automate this check, I would like to avoid having to add empty .cpp files for every &...
3
votes
1
answer
242
views
Why does the C++ Core Guideline C.46 recommend explicit only for single-argument constructors?
According to the C++ Core Guidelines, rule C.46:
By default, declare single-argument constructors explicit.
The reason is to avoid unintended conversions.
However, why is the explicit keyword ...
2
votes
1
answer
166
views
Should we still always std::forward a universal reference argument even if unnecessary?
Consider the following code:
#include <string>
auto f1(auto&& v) {
return v;
}
auto f2(auto&& v) {
return std::forward<decltype(v)>(v);
}
int main() {
return ...
4
votes
2
answers
236
views
cppcoreguidelines-virtual-class-destructor ... complains when Base has default protected destructor
We are using clang-tidy to do static analysis, and we've enabled cpp-core-guidelines.
As per previous advice (by one of the authors of cpp-core-guidelines), we have a abstract base class with a ...
2
votes
1
answer
173
views
Is Cpp Core Guidelines Rule F19 incomplete?
Cpp Core Guideline F19 tells us
Flag a function that takes a TP&& parameter (where TP is a template type parameter name) and does anything with it other than std::forwarding it exactly once ...
3
votes
1
answer
306
views
Why does range-for decay this array into pointer according to clang-tidy?
Clang-tidy (versions 13.x and 16.x) detects violations of cppcoreguidelines-pro-bounds-array-to-pointer-decay in a weirdly specific situation: when iterating over an array that is accessed through a ...
-3
votes
1
answer
366
views
Why does C++ Core Guidelines prefer simplest “singleton” as a function but not as a static member function?
C++ Core Guidelines, I.3: Avoid singletons:
Exception You can use the simplest “singleton” (so simple that it is often not considered a singleton) to get initialization on first use, if any:
X& ...
1
vote
1
answer
130
views
How to implement dangling-pointer warning in custom string type
The following code is invalid because it takes a pointer into a temporary object (triggering -Wdangling-gsl):
static std::string f() {
return "hi";
}
void func() {
const char* ptr = ...
4
votes
1
answer
382
views
Cpp Core Guidelines: "const char *" to "const uint8_t *" without reinterpret_cast and C-style cast?
For code like this:
#include <cstdint>
extern const char *f();
extern void g(const uint8_t *);
int main()
{
const char *p = f();
g(reinterpret_cast<const uint8_t *>(p));
}
clang-...
0
votes
2
answers
261
views
Is gsl::owner usable for shared-ownership?
For example, can it be used in Qt for the following?
gsl::owner<QWidget*> w{new QWidget{parent}}
In this example, the ownership is shared by the new-site and the parent, because the code who ...
1
vote
1
answer
1k
views
Why do the C++ Core Guidelines not recommend to use std::optional over pointers when approriate? [closed]
In the C++ Core Guidelines std::optional is only referred once:
If you need the notion of an optional value, use a pointer, std::optional, or a special value used to denote “no value.”
Other than ...
0
votes
1
answer
144
views
Expressing ideas directly in code - what does the definition mean?
Im new to c++, I'm reading the core guidelines and I came across this:
P.1: Express ideas directly in code
In this, it says to use something like Month month() const; instead of int month();
So I have ...
1
vote
1
answer
344
views
How can I decay const char that is passed as reference to a function with variadic parameters?
I have a function like this:
void column(const std::string &value) { ... }
void column(float value) { ... }
template <class... TColumns> void row(const TColumns &...columns) {
ImGui::...