Skip to main content
Filter by
Sorted by
Tagged with
6 votes
2 answers
282 views

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 ...
kingsjester's user avatar
8 votes
2 answers
274 views

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 ...
Enlico's user avatar
  • 30.3k
3 votes
1 answer
260 views

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 &...
darune's user avatar
  • 11.5k
3 votes
1 answer
242 views

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 ...
toliveira's user avatar
  • 1,857
2 votes
1 answer
166 views

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 ...
xmllmx's user avatar
  • 44.6k
4 votes
2 answers
235 views

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 ...
Werner Erasmus's user avatar
2 votes
1 answer
173 views

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 ...
pasbi's user avatar
  • 2,191
3 votes
1 answer
306 views

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 ...
OLEGSHA's user avatar
  • 728
-3 votes
1 answer
366 views

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& ...
3CEZVQ's user avatar
  • 42.9k
1 vote
1 answer
130 views

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 = ...
StilesCrisis's user avatar
  • 16.4k
4 votes
1 answer
380 views

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-...
user1244932's user avatar
  • 8,282
0 votes
2 answers
261 views

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 ...
Johannes Schaub - litb's user avatar
1 vote
1 answer
1k views

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 ...
Henk's user avatar
  • 866
0 votes
1 answer
144 views

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 ...
Johnathon's user avatar
  • 175
1 vote
1 answer
344 views

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::...
Gasim's user avatar
  • 8,051
15 votes
2 answers
6k views

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 ...
Bruce Adams's user avatar
  • 5,819
2 votes
1 answer
1k views

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 ...
ThreeStarProgrammer57's user avatar
1 vote
1 answer
310 views

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 ...
Andrew Truckle's user avatar
1 vote
1 answer
622 views

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 ...
Andrew Truckle's user avatar
2 votes
1 answer
481 views

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 ...
evolved's user avatar
  • 2,300
0 votes
0 answers
37 views

(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 ...
Enlico's user avatar
  • 30.3k
3 votes
0 answers
299 views

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, ...
Ortwin's user avatar
  • 31
4 votes
2 answers
387 views

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 ...
Lorah Attkins's user avatar
0 votes
1 answer
175 views

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, ...
KHALED ISMAEEL's user avatar
1 vote
0 answers
318 views

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. ...
user3768612's user avatar
5 votes
1 answer
3k views

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 = ...
fabian's user avatar
  • 1,881
-1 votes
1 answer
242 views

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: #...
nyarlathotep108's user avatar
1 vote
1 answer
1k views

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 ...
Leedehai's user avatar
  • 4,050
2 votes
0 answers
2k views

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 ...
mt_xing's user avatar
  • 684
1 vote
2 answers
2k views

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 ...
Adomas Baliuka's user avatar
16 votes
3 answers
6k views

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 ...
M47's user avatar
  • 471
3 votes
1 answer
3k views

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 (...
Diavolo's user avatar
  • 61
6 votes
4 answers
2k views

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 ...
user1032677's user avatar
3 votes
1 answer
2k views

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 ...
Phil's user avatar
  • 6,284
5 votes
2 answers
240 views

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, ...
rustyhu's user avatar
  • 2,235
2 votes
1 answer
107 views

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" ≈...
Yksisarvinen's user avatar
  • 23.3k
10 votes
1 answer
11k views

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::...
Ravi's user avatar
  • 309
6 votes
3 answers
4k views

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). ...
Timo's user avatar
  • 9,985
0 votes
2 answers
443 views

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 ...
Razvanip's user avatar
3 votes
3 answers
4k views

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 ...
Mansoor Ahmed Memon's user avatar
0 votes
1 answer
2k views

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, ...
Simon Richter's user avatar
4 votes
1 answer
1k views

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() = ...
Unimportant's user avatar
  • 2,096
1 vote
1 answer
217 views

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 ...
Martin Fehrs's user avatar
  • 1,175
3 votes
2 answers
576 views

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 : ...
Jepessen's user avatar
  • 12.7k
3 votes
1 answer
438 views

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 ...
patatahooligan's user avatar
3 votes
3 answers
1k views

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 ...
geoidiot's user avatar
  • 117
4 votes
1 answer
2k views

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 ...
Calmarius's user avatar
  • 19.7k
2 votes
2 answers
2k views

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* ...
Matt Kline's user avatar
  • 10.6k
13 votes
2 answers
1k views

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 ...
463035818_is_not_an_ai's user avatar
1 vote
4 answers
376 views

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 ...
marko1777's user avatar
  • 111