cpp-core-guidelines

Don't use static cast for arithmetic conversions (cpp-core-guidelines)

霸气de小男生 提交于 2021-01-28 04:22:53
问题 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 snippet static_cast<IntType>(static_cast<unsigned long long>(hexValue(digit)) << (digitIdx * 4)); Why shouldn't I use static_cast here? Also, with brace init this would look like this IntType{unsigned long long{hexValue(digit)} << (digitIdx * 4)}; which doesn't look any better imo. This looks

gsl::suppress whole include statements

孤街浪徒 提交于 2020-03-16 06:14:06
问题 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 where I get warnings in sdl_stdinc.h . I made sure that I include SDL only via one header under my control: ExtSDL.hpp #pragma once #pragma warning(disable: 4710) #pragma warning(push, 0) #include <SDL.h> #pragma warning(pop) I can not find

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

自古美人都是妖i 提交于 2020-02-04 02:54:29
问题 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 null-terminated since C++11? Any illustrating situations will be highly appreciated. 回答1: span("Hi") is {'H', 'i', '\0'} whereas string_span("Hi") is {'H', 'i'} . string_span checks for the terminating null character and does not include it in the span.

How can a compiler generated default constructor be more efficient than a self-written one that does nothing but initialize members? [duplicate]

你离开我真会死。 提交于 2020-01-03 11:32:07
问题 This question already has answers here : Why would the implicitly generated constructor (et al.) be more efficient than a user-defined (trivial) one? (4 answers) Are user-defined default constructors less efficient? (2 answers) Closed 7 months ago . 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

How can a compiler generated default constructor be more efficient than a self-written one that does nothing but initialize members? [duplicate]

柔情痞子 提交于 2020-01-03 11:31:52
问题 This question already has answers here : Why would the implicitly generated constructor (et al.) be more efficient than a user-defined (trivial) one? (4 answers) Are user-defined default constructors less efficient? (2 answers) Closed 7 months ago . 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

boost::any_range<gsl::string_span<>> crash in Release mode

眉间皱痕 提交于 2019-12-23 12:38:03
问题 I'm observing a rather weird behaviour of the following piece of code: #include <boost/range/adaptor/transformed.hpp> #include <boost/range/any_range.hpp> #include <vector> #include <string> #include <iostream> #include "gsl.h" template <typename T> using ImmutableValueRange = boost::any_range<T, boost::bidirectional_traversal_tag, /*const*/ T>; template <typename T, typename C> ImmutableValueRange<T> make_transforming_immutable_range(const C& container) { return container | boost::adaptors:

Understanding gsl::narrow implementation

牧云@^-^@ 提交于 2019-12-17 20:23:02
问题 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 <class T, class U> T narrow(U u) noexcept(false) { T t = narrow_cast<T>(u); if (static_cast<U>(t) != u) gsl::details::throw_exception(narrowing_error()); if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{}))) // <-- ??? gsl::details:

When do I use “__attribute__((nonnull))” vs “not_null<T*>”?

为君一笑 提交于 2019-12-10 13:59:11
问题 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, there is also the not_null<T*> wrapper type. void function1(gsl::not_null n); void f(gsl::not_null<int*> n); int main(){ int* ptr = new int(1); f(ptr); } void f(gsl::not_null<int*> n){/*impl*/} Assuming the language facilities are there to support the GSL

Ensures() - guideline support library

好久不见. 提交于 2019-12-08 17:28:18
问题 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 variable is not equal to 255 , the program crashes with the following output "terminate called without an active exception" . My question is how to use Ensures() properly? 回答1: Are you using the Microsoft GSL implementation? Then if you check the gsl_assert.h header file

What is gsl::multi_span to be used for?

假如想象 提交于 2019-12-04 06:49:09
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 multi_span { ... }; So, obviously this is some sort of a multi-dimensional version of gsl::span . But what is that supposed to mean? Why do we need this multi-dimensional span, or rather - when would we use it? I can't seem to find any documentation on this. In short, it is a span over contiguous piece of memory, which represents multidimensional array. Here is an