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
235
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
380
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::...
15
votes
2
answers
6k
views
Why hasn't not_null made it into the C++ standard yet?
After adding the comment "// not null" to a raw pointer for the Nth time I wondered once again whatever happened to the not_null template.
The C++ core guidelines were created quite some ...
2
votes
1
answer
1k
views
How to prevent ODR violations in this case?
disclaimer: this question is about prevention of unintended naming collisions, and make sure the following code fail to compile/link.
[edit] actually I'd be happy with something preventing this to ...
1
vote
1
answer
310
views
Using gsl::narrow fails
I know there are similar questions and I don't know the best wording for this one.
I find it a little ironic that the reason for the code analysis warning in the first place was that it told me to use ...
1
vote
1
answer
622
views
How do I define __cpp_exceptions for gsl:narrow to compile?
I am getting confused again :(
I have looked at this discussion:
detect at compile time whether exceptions are disabled
I am new to trying to use GSL. I have copied the GSL folder to my PC and added a ...
2
votes
1
answer
481
views
CppCoreGuidlines R.33 Why pass `unique_ptr` by reference?
The CppCoreGuidlines rule R.33 suggests to
Take a unique_ptr<widget>& parameter to express that a function
reseats the widget.
Reason Using unique_ptr in this way both documents and ...
0
votes
0
answers
37
views
About using directives in namespace in header files [duplicate]
(This is kind of related to a previous question.)
Core guideline SF.7 gives a good motivation for avoiding to put using namespace directives at global scope in a header file.
However, even writing ...
3
votes
0
answers
299
views
Why do I get the warning: Don't dereference a pointer that may be invalid
With C++ Core-Guidelines lifetime checking enabled (Visual Studio 2019), the following code produces a warning I don't understand:
std::list<std::string> getKeys(const std::map<std::string, ...
4
votes
2
answers
387
views
Are you meant to recover from contract violations?
With the guideline support library and utilities like gsl_Expects C++ implements contracts for the time being (there are plans to bake this stuff into the language in the future). Using this feature ...
0
votes
1
answer
175
views
Why do C++ regular expression functions use output parameters?
Output parameters in C++ are generally considered a code smell according to the core guidelines. Yet, we have such functions in the regular expressions library
template< class BidirIt,
...
1
vote
0
answers
318
views
C++ Core Guidelines: Lifetime - why does the following code trigger a warning?
When compiling the following code in Visual Studio 2019 while using the "C++ Core Check Lifetime Rules", I get
warning C26486: Don't pass a pointer that may be invalid to a function. ...
5
votes
1
answer
3k
views
Why is this dangling-gsl warning invoked?
I am analyzing a codebase with clang-tidy and see a warning I do not understand. The warning is invoked by the following lines of code:
void fun(const QString& bar) {
const char* c_format = ...
-1
votes
1
answer
242
views
Include directives order in source file and in header file [closed]
Which is the recommended order in which the #include directives are supposed to be listed? I could not find any answer in the C++ Core Guidelines
For example, should they be ordered like this:
#...
1
vote
1
answer
1k
views
Does gsl::not_null hurt performance?
C++ Core Guidelines recommends a gsl::not_null type. As stated in I.12: Declare a pointer that must not be null as not_null:
To help avoid dereferencing nullptr errors. To improve performance by
...
2
votes
0
answers
2k
views
Clang-Tidy Narrowing Conversion from uint8_t to float
I'm getting a clang-tidy warning that reads narrowing conversion from 'int' to 'float' when I convert from a uint8_t to a float, which to my understanding is not a narrowing conversion since float can ...
1
vote
2
answers
2k
views
Defining interfaces (abstract classes without members) in C++
By an interface (C# terminology) I mean an abstract class with no data members. Thus, such a class only specifies a contract (a set of methods) that sub-classes must implement. My question is: How to ...
16
votes
3
answers
6k
views
C++ Core Guidelines for static member variables
I have a private static vector in my class that keeps a pointer to all objects created from it. It's necessary as each object needs access to information from all the other objects to perform some ...
3
votes
1
answer
3k
views
How to use gsl narrow cast
I am trying to understand how to use gsl::narrow_cast instead of static_cast.
I found here on stackoverflow a function that has a string as a parameter and returns true if all characters are ASCII (...
6
votes
4
answers
2k
views
How to properly use "C++ Core Guidelines: C.146: Use dynamic_cast where class hierarchy navigation is unavoidable"
Motivation
The C++ Core Guidelines recommends using dynamic_cast when "class hierarchy navigation is unavoidable." This triggers clang-tidy to throw the following error: Do not use ...
3
votes
1
answer
2k
views
Error Handling with Core Guidelines GSL Expects, Ensures, and narrow_cast
I am trying to follow the Cpp Core Guidelines and use GSL where appropriate. In particular, I would like to use Expects and Ensures for pre and post-conditions, as well as span, and narrow_cast, but ...
5
votes
2
answers
240
views
In C++ Core Guidelines Per.4, why is the bad example intended to be faster?
I am reading this recently, which states:
Don’t assume that complicated code is necessarily faster than simple code.
The code is copied as following:
Example, good
// clear expression of intent, ...
2
votes
1
answer
107
views
CppCoreGuidelines: What are hot int copies?
I've been reading CppCoreGuidelines F.15 and I don't understand the following sentences from the table of parameter passing:
"Cheap" ≈ a handful of hot int copies
"Moderate cost" ≈...
10
votes
1
answer
11k
views
C++ Warning: Assigning newly created gsl::owner<> to non-owner
When I use the following code, I get a warning (From applying cppcoreguideline).
Code:
SampleClass *object = nullptr;
object = new SampleClass();
Warning:
warning: assigning newly created 'gsl::...
6
votes
3
answers
4k
views
Don't use static cast for arithmetic conversions (cpp-core-guidelines)
msvc's code analyzer for the cpp core guidelines tells me
Warning C26472 Don't use a static_cast for arithmetic conversions. Use
brace initialization, gsl::narrow_cast or gsl::narrow
(type.1).
...
0
votes
2
answers
443
views
Assert is seen as C style cast in Visual Studio
Here is the error and a glimpse of the code One of my courses demands me to use Warning Level 4 and to treat warnings as errors in Visual Studio. Beside that, we also need to activate Cpp Core ...
3
votes
3
answers
4k
views
C++ Discussion: Use of =, {}, and () as initializers, Which one should I use?
While reading the C++ Core Guidelines by isocpp I came through this section. I have seen these methods in some of the C++ code I have read so far. For example: the () have been used while initializing ...
0
votes
1
answer
2k
views
How to "reset" gsl::owner<T>?
When I create an object and append it to a list
auto o = new object;
m_objects.push_back(o);
I get several hints from the compiler that I should clean up my code along the C++ Core Check guidelines, ...
4
votes
1
answer
1k
views
Is the C++ core guideline C35 "A base class destructor should be either public and virtual, or protected and nonvirtual" wrong for interface classes?
Say I have the following interface class in order to "Write to interfaces, not implementations":
class IDrawable
{
public:
virtual void
Draw() const = 0;
protected:
~IDrawable() = ...
1
vote
1
answer
217
views
How to handle static analysis warning from Core Guidelines checker about gsl::at?
I activated static analysis for my project in Visual Studio. The Core Guidelines checker says i should use gsl::at for subscription. But my code is save. What's the cleanest way to get rid of this ...
3
votes
2
answers
576
views
Problem with operator inheritance and cpp core guidelines c.128
I've following code (I've removed some code not important here):
class State {
public:
virtual void enter() = 0;
virtual void update() = 0;
virtual void exit() = 0;
};
class SimpleState : ...
3
votes
1
answer
438
views
When using lambdas for complex initialization of variables, how can I handle outside of the lambda exceptions that are thrown from inside?
I am using lambdas to initialize some const variables as described in the core c++ guidelines here. In short, the idiom looks like this
const auto a = [&]() {
MyType a;
// complex ...
3
votes
3
answers
1k
views
What is an 'aliased local shared_ptr' in this example
I have a question about an example in the Cpp Core Guidelines. In R.37: Do not pass a pointer or reference obtained from an aliased smart pointer there is the following example:
// global (static or ...
4
votes
1
answer
2k
views
How can I use unions without clang-tidy warnings?
Clang-tidy's cppcoreguidelines-pro-type-union-access rule is essentially a complete ban on unions, it flags all access to union members.
My library has an extern "C" interface with a structure which ...
2
votes
2
answers
2k
views
Using gsl::zstring_view with C APIs
I'm trying to use modern string-handling approaches (like std::string_view or GSL's string_span) to interact with a C API (DBus) that takes strings as null-terminated const char*s, e.g.
DBusMessage* ...
13
votes
2
answers
1k
views
How can a compiler generated default constructor be more efficient than a self-written one that does nothing but initialize members?
Triggered by this answer I was reading in the core guidelines:
C.45: Don’t define a default constructor that only initializes data
members; use in-class member initializers instead
The reasoning ...
1
vote
4
answers
376
views
"Moving" sequential containers to pointers
I'm building a buffer for network connections where you can explicitly allocate memory or you can supply it on your own via some sequential container(eg.:std::vector,std::array)these memory chunks are ...