Questions tagged [cpp-core-guidelines]

The C++ Core Guidelines are a collaborative effort led by Bjarne Stroustrup, much like the C++ language itself. They are the result of many person-years of discussion and design across a number of organizations. Their design encourages general applicability and broad adoption but they can be freely copied and modified to meet your organization's needs.

The C++ Core Guidelines, managed from this GitHub repository, represent a recent attempt to define good practices for modern C++ development. The guidelines focus on rules which static analysis tools are capable of detecting.

Alongside the guidelines themselves is the guideline support library (GSL). While the guidelines do not go into explicit detail about the behavior of such features, the Microsoft implementation currently acts as the de-facto standard.

94 questions
5
votes
1 answer

How can I use gsl::span and indicate ownership?

I want to write a function which: Takes a pointer as a parameter Takes a length as a parameter Owns the memory pointed to by the pointer (e.g. maybe it releases it, or constructs a unique_ptr to it in some data structure etc.) Now, if I wanted 1+2…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
5
votes
2 answers

array_view alternative for maps, sets, etc

Let's assume I have some class hierarchy that has a couple of virtual functions returning a container reference: #include #include #include #include #include class Interface { public: virtual…
Rostislav
  • 3,857
  • 18
  • 30
4
votes
2 answers

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 and depending on your project setup, contract…
Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
4
votes
1 answer

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() = default; }; Clients should not be able to delete…
4
votes
1 answer

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 contains an union. I cannot use variants in…
Calmarius
  • 18,570
  • 18
  • 110
  • 157
4
votes
1 answer

Why gsl::not_null ensures ptr is not null on get()?

In the Microsoft implementation of guidelines support library I see the following piece of code: template class not_null { ... template ::value>> constexpr…
alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51
4
votes
3 answers

boost::any_range> crash in Release mode

I'm observing a rather weird behaviour of the following piece of code: #include #include #include #include #include #include "gsl.h" template
Rostislav
  • 3,857
  • 18
  • 30
3
votes
0 answers

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 getKeys(const std::map& entities) { std::list rc; for…
Ortwin
  • 31
  • 2
3
votes
2 answers

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 : public State { public: SimpleState() = default; …
Jepessen
  • 11,744
  • 14
  • 82
  • 149
3
votes
1 answer

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 initialization return a; }(); The problem arises when…
patatahooligan
  • 3,111
  • 1
  • 18
  • 27
3
votes
3 answers

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 heap), or aliased local ... shared_ptr g_p…
geoidiot
  • 107
  • 2
  • 12
3
votes
2 answers

C++: attempt to eliminate raw loop with equivalent STL algorithm

I'm trying to modernize some C++ code, adhering to core guidelines and post ++11 advice. The specific guideline I'm addressing here is to use facilities in lieu of raw loops applying static operations across a sequence with the aim of…
schulmaster
  • 413
  • 5
  • 16
3
votes
2 answers

gsl::suppress whole include statements

I am integrating Guideline Support Library Checkers into a project of mine. Microsoft.CppCoreCheck Microsoft.Gsl When I run it I get a bunch of errors from included libraries like standard libraries, glm, boost, etc. One concrete example is SDL.h…
Johannes
  • 6,490
  • 10
  • 59
  • 108
3
votes
0 answers

How can you exclude third-party libraries from CppCoreCheck?

I started to use CppCoreCheck on some of my projects. Hurray! But now I'm stuck in a gazillion warnings from boost and Qt. Is there a way to exclude them from the check?
xtofl
  • 40,723
  • 12
  • 105
  • 192
3
votes
2 answers

gsl::span and gsl::span overloads are ambigous

C++ Core Guidelines promotes the practice of using span. The problem is with const and mutable ranges. This is what I tried to do: auto foo(gsl::span); // 1st auto foo(gsl::span); // 2nd But they can't be called without…
bolov
  • 72,283
  • 15
  • 145
  • 224