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
411
votes
4 answers

What is a "span" and when should I use one?

Recently I've gotten suggestions to use span's in my code, or have seen some answers here on the site which use span's - supposedly some kind of container. But - I can't find anything like that in the C++17 standard library. So what is this…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
103
votes
3 answers

What's the difference between span and array_view in the gsl library?

In several recent conference presentation I've heard Bjarne Stroustrup and others mention new coding guidelines for C++ and some types supporting them. Specifically, I remember the example of span instead of (T* p, int n) as a parameter to a…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
76
votes
2 answers

gsl::not_null vs. std::reference_wrapper vs. T&

C++ Core Guidelines has been presented recently (congrats!) and I am concerned about 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 …
Mikhail
  • 20,685
  • 7
  • 70
  • 146
21
votes
2 answers

Units for types in C++

In the C++ Core Guidlines P.1 change_speed example, it shows a Speed type that is used as shown below: change_speed(Speed s); // better: the meaning of s is specified // ... change_speed(2.3); // error: no unit change_speed(23m / 10s); // meters per…
rozzy
  • 2,828
  • 6
  • 25
  • 35
18
votes
3 answers

Why can't I construct a gsl::span with a brace-enclosed initializer list

According to the C++ Core Guidelines, I should use a gsl::span to pass a half-open sequence. I think that means that instead of writing a function like: void func(const std::vector& data) { for (auto v : data) std::cout << v << " "; } I…
user3467895
  • 572
  • 4
  • 13
15
votes
2 answers

Is CppCoreGuidelines C.21 correct?

While reading the Bjarne Stroustrup's CoreCppGuidelines, I have found a guideline which contradicts my experience. The C.21 requires the following: If you define or =delete any default operation, define or =delete them all With the following…
alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51
13
votes
2 answers

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 given is Reason Using in-class member initializers…
13
votes
2 answers

How to use C++ Expects operator?

I'm starting a project using C++, which I haven't used before outside of a handful of school projects - nowhere near the scope of what I'm tackling now. My goal is to try my best to follow the C++ Core Guidelines as I work to avoid errors, improve…
stevendesu
  • 15,753
  • 22
  • 105
  • 182
12
votes
3 answers

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 calculations: // Header file: class Example…
M47
  • 400
  • 2
  • 13
12
votes
2 answers

Understanding gsl::narrow implementation

The C++ Core Guidelines has a narrow cast that throws if the cast changes the value. Looking at the microsoft implementation of the library: // narrow() : a checked version of narrow_cast() that throws if the cast changed the value template
bolov
  • 72,283
  • 15
  • 145
  • 224
11
votes
5 answers

What is wasted in this example from the Cpp Core Guidelines?

What is wasted in the example from the Cpp Core Guidelines? P.9: Don't waste time or space [...] void lower(zstring s) { for (int i = 0; i < strlen(s); ++i) s[i] = tolower(s[i]); } Yes, this is an example from production code. We leave it to…
hamster on wheels
  • 2,771
  • 17
  • 50
11
votes
1 answer

When do I use "__attribute__((nonnull))" vs "not_null"?

I'm accustomed to using __attribute__((nonnull)) when expressing pointers that should not be null. void f(int* ptr) __attribute__((nonnull)); int main(){ int* ptr = new int(1); f(ptr); } void f(int* ptr){/*impl*/} However, with the GSL,…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
8
votes
1 answer

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::owner<>' to non-owner 'SampleClass *'…
Ravi
  • 179
  • 3
  • 14
8
votes
1 answer

Integration of CMake with the Visual Studio 2017 C++ Core Guidelines Checker (CppCoreCheck)

I currently try to integrate the C++ Core Guidelines Checker (CppCoreCheck) of Visual Studio 2017 with the meta build system CMake using the Visual Studio 15 2017 Win64 generator. The following example illustrates my approach (I've tried to strip…
8
votes
8 answers

Do not use array subscript when the index is not an integer constant expression; use gsl::at() instead

I was trying to create some example code in Microsoft Visual Studio which looks like that int main() { const size_t size = 10; int arr[size]; for (size_t i = 0; i < size; ++i) arr[i] = i; return 0; } Now JetBrains…
user4290866
1
2 3 4 5 6 7