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
2
votes
1 answer

Is there a GSL implementation I can use with GCC 4.9.x?

Microsoft's (Core) Guidelines Support Library implementation is said to support GCC 5.1 - but does not specify support for other versions. Higher versions seem to be ok (anyway, 5.3.1 on my Debian Stretch) - but building the tests with GCC 4.9.3…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
2
votes
1 answer

Is there a legal way to move from gsl::not_null?

The Guidelines Support Library introduced not_null who's purpose is to enforce an invariant on pointer-like types, advertently on smart pointers. However it's a known issue that not_null> doesn't work. As far as I see the reason is…
2
votes
1 answer

Suggestions for returning memory from a class

I have a class which is supposed to keep pixel data (floats for the position, floats for the color). I'm trying to use a C++ style in data members (the data is kept in std::array instead of plain C arrays). The class has other getters,…
Dean
  • 6,610
  • 6
  • 40
  • 90
2
votes
1 answer

gsl::array_view> from std::vector

Assume I have a member variable std::vector in a class and I want to return it from a member function as an immutable view using a combination of gsl::array_view and gsl::cstring_view. Unfortunately, the following doesn't compile: class…
Rostislav
  • 3,857
  • 18
  • 30
1
vote
1 answer

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 = f().c_str(); } :8:23: warning: object backing the…
StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
1
vote
1 answer

Why do the C++ Core Guidelines not recommend to use std::optional over pointers when approriate?

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 that, it is not mentioned in the guidelines, so in…
Henk
  • 826
  • 3
  • 14
1
vote
1 answer

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 void row(const TColumns &...columns) { ImGui::TableNextRow(); (column(columns), ...); } I am using…
Gasim
  • 7,615
  • 14
  • 64
  • 131
1
vote
1 answer

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 gsl::narrow into two instances: Instance…
1
vote
1 answer

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 avoiding redundant checks for nullptr. ... By stating…
Leedehai
  • 3,660
  • 3
  • 21
  • 44
1
vote
2 answers

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 implement such a class correctly in modern…
Adomas Baliuka
  • 1,384
  • 2
  • 14
  • 29
1
vote
1 answer

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 warning? Should I disable it? Should I write my code…
1
vote
4 answers

"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 stored in a list what we use later for…
marko1777
  • 51
  • 2
  • 7
1
vote
1 answer

Microsoft VS2015 C++ Core Guidelines Analysis: "no array to pointer decay (bounds.3)" for vtable?

When I run the CppCoreCheck code analysis on my VS2015 project, I get a number of warnings which seem "unfixable" because they refer to the underlying C++ implementation of classes and vtables: An example class: // Header file class IMyClass…
BTownTKD
  • 7,911
  • 2
  • 31
  • 47
1
vote
2 answers

Modifying not_null to disallow comparisons to nullptr

I'm trying to get a compile-time check for the assignment and testing against null. Reason is I'm using a "magic" non-null pointer to represent a disengaged state for something, and it's easy to forget it's using that magic pointer and mistakenly…
1
vote
1 answer

Cpp Core Guidelines Checker using console and MSBuild

I have solution with some projects that are set up for building. I want to run guideline checkers for solutions. Using this MSDN article I don't want to modify project files, so I created batfile: call "%VS140COMNTOOLS%\VsMSBuildCmd.bat" msbuild…