Questions tagged [guideline-support-library]

The Guidelines Support Library (GSL) contains functions and types that are suggested for use by the C++ Core Guidelines maintained by the Standard C++ Foundation.

41 questions
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
29
votes
2 answers

What are the C++ GSL guidelines?

Stroustrup gave a talk last year about his GSL (Guideline Support Library). There is an implementation by Micosoft at https://github.com/Microsoft/GSL . I was under the impression that the GSL was supposed to advise on bad coding style, and suggest…
blippy
  • 1,490
  • 1
  • 13
  • 22
10
votes
1 answer

Advantage of gsl assert vs assert in c++?

I know the use of assert in C++. Wanted to know is there any difference between and any benefit(I think assert is costlier according as mentioned in https://www.learncpp.com/cpp-tutorial/7-12a-assert-and-static_assert/ so performance wise, are both…
suresh m
  • 585
  • 5
  • 17
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
2 answers

What does narrow_cast do?

I saw a code that used narrow_cast like this int num = narrow_cast(26.72); cout << num; The problem is my compiler said: 'narrow_cast' was not decleared in this scope. Am I supposed to define narrow_cast myself or am I using it the wrong…
Lekan
  • 115
  • 1
  • 6
7
votes
1 answer

Understanding the need for this `const&` specialization

In the Guidelines Support Library there is a class called final_action (essentially the well known ScopeGuard). There are 2 free-standing convenience functions to generate this templated class: // finally() - convenience function to generate a…
Baruch
  • 20,590
  • 28
  • 126
  • 201
7
votes
0 answers

Avoiding loss of __restrict__ when using GSL spans

I (mostly) like the new C++ Core Guidelines initiative, and what the Guidelines Support Libary offers. Specifically, I want to use spans more. However, I'm coming up against the issue of __restrict__ not being part of C++ while I want to/need to…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
7
votes
1 answer

Using gsl::span with range-v3

I tried a little example to get used to the GSL and range-v3 libraries and I wondered how they could work together. I have this toy example #include #include using namespace std; using namespace ranges; void…
Maikel
  • 1,204
  • 7
  • 19
6
votes
1 answer

Difference between std::basic_string_view and std::span

I'm parsing a binary network data and i would like to make the process as allocation-less as possible. But now i realized there are 2 very similar concepts that are probably both good enough for my case, and those are std::basic_string_view and…
Youda008
  • 1,788
  • 1
  • 17
  • 35
6
votes
1 answer

How do gsl::string_span and std::string_view differ?

From what I can gather, gsl::string_span and std::string_view seem to have essentially the same rationale for use. Is that indeed the case? If so, are they effectively identical? If not - how do they differ? Related question: What purpose does…
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
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); //…
5
votes
1 answer

How can I use gsl::span and indicate ownership?

I want to write a function which: Takes a pointer as a parameter Takes a length as a parameter Owns the memory pointed to by the pointer (e.g. maybe it releases it, or constructs a unique_ptr to it in some data structure etc.) Now, if I wanted 1+2…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
5
votes
3 answers

Should I replace (void*, size) with a GSL span?

Suppose I have int foo(void* p, size_t size_in_bytes); and assume it doesn't make sense to make foo typed. I want to be a good coder and apply the C++ core guidelines. Specifically, I want to use spans instead of (*, len) pairs. Well, span
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1
2 3