What are the C++ GSL guidelines?

31.3k Views Asked by At

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?
2

There are 2 best solutions below

6
On

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.

0
On

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