What are the C++ GSL guidelines?
Asked Answered
L

2

30

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 improvements.

To this end, I installed MSFT's GSL and created a C++ file:

#include <stdio.h>
#include <gsl.h>

int main()
{
        int *i = new int;
        puts("hello world");
} 

and built it using the Makefile:

msft : msft.cc
        g++ -std=gnu++14 -I ../../src/GSL/include $^ -o $@

.PHONY : clean
clean :
        rm -f msft

Obviously, there is a resource leak in the code caused by the new.

So now I'm confused.

  • What is the GSL supposed to actually do?
  • Where can I get the source code checker that warns of guideline non-compliance? Stroustrup seemed to imply that it actually exists as a tool, but is that the case?
Leone answered 2/5, 2016 at 11:7 Comment(3)
well, at least they're honest.. "assumes a platform that implements C++14 support. There are specific workarounds to support MSVC 2013 and 2015." To be fair, my understanding is to support c++11 they have to completely rebuild their entire compiler... but it's been 5 years and they still have fundamental shortcomings. Expression sfinae? not supported.Independent
I don't believe this close was appropriate. This question is asking how to use the GSL, which is a perfectly good StackOverflow question. It only asks for a recommendation for a tool in passing, in the last bullet point, which could trivially be deleted.Yesterday
"So now I'm confused." Okay, now I'm confused. I was expecting the outcome of make to be posted, but it's not there.Chancellorsville
A
14

The Guidelines Support Library (see also gsl-lite as an alternative) is a C++ library that implements some of the functions and classes recommended in the C++ Core Guidelines. A document with advice on how to use modern C++. It is worthwhile reading or skimming over the C++ Core Guidelines if you want to improve your use of C++. Using the GSL library is less important, but could be useful if you find yourself implementing code that is already in it. The C++ Core Guidelines have been around for a few years now, so some things, such as string_view, are already available (depending on what version of C++ you are compiling to) and do not require an external library to use.

Ariadne answered 2/12, 2017 at 14:37 Comment(6)
Note that only half or even less of the guidelines will improve your C++. The other half will just make it worse.Alexipharmic
and how is that?Monitory
Even if one acts differently than suggested in the guidelines (because of one's style, needs, environment, philosophy, etc.), one can perhaps do a more conscious decision about it.Hardee
@PabloAriel Pretty bold statement you're making here given the expertise of the people writing those guidelines. Please give me at least on guideline that makes your coding in C++ worse and why you think so.Megaera
@ElmarZander I think so because of the code bloat. The code size is an actual metric which relates to development times and effort quite a lot. I also code a lot, like in more than 16 hs a day sometimes for over many years and I learn the best from everyone I can, not just from Stroutsoup as he is right in some things and wrong in others, just like me and everyone else, which means that I agree with the mentioned Guidelines, except when I don't because I know others which are better and in conflict with them.Alexipharmic
@PabloAriel Still, no example given and, if really half or more than half of them are bad, then it should be easy to give one. When you talk about code bloat you might be talking about guidelines like this one [github.com/isocpp/CppCoreGuidelines/blob/master/…, introducing a new function to do some initialization. But the guideline in itself is also not bad - you have to balance bloat vs maintainability. Something you have to think about and decide for each case - the guideline just makes you think about.Megaera
W
5

You must use them as suggested in the CppCoreGuidelines.

Read them, understand how it applies to your codebase/programming habits/problems.

Visual Studio 2015 has plugins which help you to check if your code behaves well according to GSL

Westbrooks answered 2/5, 2016 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.