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
8
votes
2 answers

owner p syntax in cpp core guidelines

In cpp core guidelines: Example of non owning raw pointer I do not understand the following code: template class X2 { // ... public: owner p; // OK: p is owning T* q; // OK: q is not owning }; what is this…
arenard
  • 652
  • 6
  • 12
8
votes
1 answer

What's the difference between passing span and std::array as arguments?

In his C++ Core Guidelines, Bjarne Stroustrup recommends using span when passing arrays by reference. Why not just pass a std::array object?
Joe Huang
  • 91
  • 1
  • 2
8
votes
1 answer

Ensures() - guideline support library

I am trying to understand how to use Ensures() in code. As given in the example, if I tried using Ensures() as follows... int main(void) { int result = 0; // Some calculation Ensures(result == 255); return 0; } If the result…
NJMR
  • 1,886
  • 1
  • 27
  • 46
7
votes
1 answer

What purpose does `gsl::string_span` aim at?

During reading Microsoft's implementation of Cpp Core Guidelines, I run across two questions: Why is gsl::string_span provided where gsl::span already works well? Why is gsl::zstring_span provided where std::string is already guaranteed to be…
szxwpmj
  • 465
  • 2
  • 10
7
votes
2 answers

Use of templates to raise the level of abstraction of code?

I'm going throught T.1 of the CppCoreGuidelines, and there are the following examples Example 1 template // requires Incrementable T sum1(vector& v, T s) { for (auto x : v) s += x; return s; } Example…
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
7
votes
1 answer

CppCoreChecker C-Style cast warning when using range based for loop on vector

Assume the following code: #include #include #include int main() { std::vector lines; lines.push_back("line"); for (const auto& s : lines) { std::cout << s; } } At the line of the…
Maximilian
  • 4,728
  • 5
  • 29
  • 28
6
votes
1 answer

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 time ago now and a few things have made into into the standard…
Bruce Adams
  • 4,953
  • 4
  • 48
  • 111
6
votes
4 answers

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 static_cast to downcast from a base to a derived class; use…
6
votes
1 answer

What is gsl::multi_span to be used for?

The C++ core guidelines mention spans, not "multi-spans". But - I see that Microsoft's GSL implementation has a multi_span class template < typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions > class…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
6
votes
2 answers

Should I return gsl::span instead of const std::vector&

I have a class with a std::vector member and a member function returning a const reference to that vector. class demo { public: //... const std::vector & test() const { return iv; } private: std::vector
5
votes
1 answer

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 = bar.toStdString().c_str(); …
fabian
  • 1,413
  • 1
  • 13
  • 24
5
votes
2 answers

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, fast execution vector v(100000); for…
rustyhu
  • 1,912
  • 19
  • 28
5
votes
3 answers

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). for this…
Timo
  • 9,269
  • 2
  • 28
  • 58
5
votes
1 answer

std::span.size() vs array/vector size

We are playing around with std::span() (using the gsl implementation for now) at work. Recently we discovered that comparing a std::span.size() to a vector.size() was giving a -Wsign-compare error: if( span.size() > vector.size() ) // comparison…
Brad
  • 5,492
  • 23
  • 34
5
votes
0 answers

How to properly install a header that includes GSL (Guidelines Support Library)

// include/MyLib/MyModel.h #include #include #include "myEntity.h" #include class MyModel { public: std::unique_ptr load(std::string id); bool store(gsl::not_null entity); //…