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
0 answers

Cpp Core Guidelines: "const char *" to "const uint8_t *" without reinterpret_cast and C-style cast?

For code like this: #include extern const char *f(); extern void g(const uint8_t *); int main() { const char *p = f(); g(reinterpret_cast(p)); } clang-tidy -checks='cppcoreguidelines-*' generates warning: do not…
user1244932
  • 7,352
  • 5
  • 46
  • 103
2
votes
1 answer

How to prevent ODR violations in this case?

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 compile/link, or something that solves this, like…
ThreeStarProgrammer57
  • 2,906
  • 2
  • 16
  • 24
2
votes
1 answer

CppCoreGuidlines R.33 Why pass `unique_ptr` by reference?

The CppCoreGuidlines rule R.33 suggests to Take a unique_ptr& parameter to express that a function reseats the widget. Reason Using unique_ptr in this way both documents and enforces the function call’s reseating semantics. Note “reseat”…
evolved
  • 1,850
  • 19
  • 40
2
votes
0 answers

Clang-Tidy Narrowing Conversion from uint8_t to float

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 represent every integer that a uint8_t can. Code…
mt_xing
  • 631
  • 7
  • 22
2
votes
1 answer

How to use gsl narrow cast

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 (first 127 characters). bool IsCleanString(const…
Diavolo
  • 41
  • 2
  • 7
2
votes
1 answer

CppCoreGuidelines: What are hot int copies?

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" ≈ memcpy hot/contiguous ~1KB and no allocation What does "hot int…
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
2
votes
3 answers

C++ Discussion: Use of =, {}, and () as initializers, Which one should I use?

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 data fields in the constructors initialization…
2
votes
2 answers

Using gsl::zstring_view with C APIs

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* dbus_message_new_method_call( const char*…
2
votes
1 answer

Should I use the Guidelines Support Library (GSL) in a new C++ project?

What are the pros and cons in favor of and against using the Guidelines Support Library (GSL) in a new C++ project? I find some constructs there very attractive but am a bit scared of including and relying on such a fundamental library.
smichak
  • 4,716
  • 3
  • 35
  • 47
2
votes
1 answer

Doesn't gsl::cstring_span support constexpr? If not, why should I use it?

I have a piece of code that looks like this: constexpr gsl::cstring_span<> const somestring{"Hello, I am a string"}; and it refuses to compile with a message complaining that some non-constexpr function is being called somewhere. Why is this? This…
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
2
votes
1 answer

How to use gsl::span to fix No array to pointer decay warning?

I am attempting to resolve the following warning. warning C26485: Expression 'pNMLink->item.szUrl': No array to pointer decay. (bounds.3...) This warning is caused by the following code. auto pNMLink =…
Benilda Key
  • 2,836
  • 1
  • 22
  • 34
2
votes
0 answers

How do I select between GSL Lite and Microsoft GSL in CMake?

I'm writing some C++ code which uses, say, gsl::span. Now, I would like to be able to use either Microsoft/Neil Macintosh's GSL, or Martin Moene's GSL Lite. How would I best accomplish that, using CMake as my built system?
einpoklum
  • 118,144
  • 57
  • 340
  • 684
2
votes
5 answers

F.54: If you capture this, capture all variables explicitly (no default capture)

Slightly got confused after reading this Cppcore Guideline F.54 Regarding the lambda capture "Writing [=] in a member function appears to capture by value, but actually captures data members by reference " Example class My_class { int x =…
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
2
votes
1 answer

Why shouldn't we use pointer arithmetic with gsl::not_null?

This is a contrived example, but consider the following: #include #include "gsl.h" int main(){ //object or array that I'd like to iterate over one byte at a time char array[] = {'a','b','c','d','e','f'}; //create a C-like…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
2
votes
1 answer

fatal error C1001: An internal error has occurred in the compiler

I tried including the gsl.h for hands on purpose in my VS2013. When I try to build the project, it says c:\users\njain6\documents\visual studio 2013\projects\gcldemo\string_span.h(336): fatal error C1001: An internal error has occurred in the…
NJMR
  • 1,886
  • 1
  • 27
  • 46