How to start using C++ Core Guidelines Checker and GSL
Tags: cpp, gsl
Published: 2018-03-04 16:58:10

---
title: How to start using C++ Core Guidelines Checker and GSL
published: true
description: This article describes how to introduce C++ Core Guidelines Checker and GSL
tags: Cpp, GSL
---

###### (A Japanese translation is available [here](https://qiita.com/TomoyukiAota/items/28b39d77646daa74291a).)

# Note
In this article, I described how to start using C++ Core Guidelines Checker. However, C++ Core Guidelines Checker has some immature points, and [clang-tidy](http://clang.llvm.org/extra/clang-tidy/) seems a better tool. Therefore, please consider using [clang-tidy](http://clang.llvm.org/extra/clang-tidy/). ([@tenmyo](https://qiita.com/tenmyo) pointed out that [clang-tidy](http://clang.llvm.org/extra/clang-tidy/) has checks based on C++ Core Guidelines.)

I'm leaving the information about C++ Core Guidelines Checker just for reference.



# Introduction

## What is C++ Core Guidelines Checker?

[C++ Core Guidelines](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) is created mainly by 
 Bjarne Stroustrup and Herb Sutter for writing better C++ code. Most of the items in the guidelines are designed to be supported by an analysis tool.

C++ Core Guidelines Checker is a static analysis tool based on [C++ Core Guidelines](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines). Microsoft is developing this tool, and Visual Studio 2017 supports it by default. This tool emits warnings for violating code in Error List window when VS builds the code. [Visual Studio C++ Core Guidelines Checker Reference | Microsoft Docs](https://docs.microsoft.com/en-us/visualstudio/code-quality/code-analysis-for-cpp-corecheck) describes what warnings to be emitted for what kind of violation.

As of May 2018, C++ Core Guidelines Checker works only on Visual Studio on Windows. Therefore, the content of this article applies only to such environment.

## What is GSL?

GSL (Guidelines Support Library) is a library which contains types and functions that are suggested by [C++ Core Guidelines](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines). The following article covers what types and functions are available.

- [C++ Core Guideline: The Guideline Support Library - ModernesCpp.com](http://www.modernescpp.com/index.php/c-core-guideline-the-guidelines-support-library)

There are several implementations of GSL. In this article, I will use [Microsoft/GSL](https://github.com/Microsoft/GSL).

GSL supports [multiple platforms/compilers](https://github.com/Microsoft/GSL#supported-platforms). I hope C++ Core Guidelines Checker will have multi-platform support as well.

# How to begin using them?

## Enable C++ Core Guidelines Checker

C++ Core Guidelines Checker has to be enabled in each VS project. The following pages cover the procedure to enable it. Also, several useful things (like disabling specific warnings) are described.

- [Using the C++ Core Guidelines checkers | Microsoft Docs](https://docs.microsoft.com/en-us/visualstudio/code-quality/using-the-cpp-core-guidelines-checkers)
- [C++ Core Guidelines Checker in Visual Studio 2017 | Visual C++ Team Blog](https://blogs.msdn.microsoft.com/vcblog/2017/08/11/c-core-guidelines-checker-in-visual-studio-2017/)
- [Managing warnings in the C++ Core Guidelines Checker | Visual C++ Team Blog](https://blogs.msdn.microsoft.com/vcblog/2017/08/14/managing-warnings-in-the-c-core-guidelines-checker/)

## Install GSL code

Copy and paste [gsl](https://github.com/Microsoft/GSL/tree/master/include/gsl) in your codebase. A package manager like Conan is preferable, but I could not find a well-maintenanced one. Hmm...

## A wrapper of GSL

Currently, C++ Core Guidelines Checker emits warnings to third-party libraries, and there are no settings to avoid it. ([Microsoft recognizes this problem and working on it.](https://stackoverflow.com/questions/45785126/selectively-disable-c-core-guidelines-checker-for-third-party-libraries))　To be specific, it emits warnings to GSL code when we use GSL. This behavior results in the flood of warnings to the code which are not mine. In order to avoid this, we need a wrapper of GSL, and we use GSL through the wrapper.

The code of the wrapper of GSL is shown below. Here, some ifdefs of `_MSC_VER` are because 1. `CppCoreCheck/warnings.h` are created during VS 2017 installation, and 2. `#pragma` directive for suppressing warnings are specific to MSVC.

```
#ifdef _MSC_VER
#if _MSC_VER >= 1910
#include <CppCoreCheck/warnings.h>
#pragma warning(disable: ALL_CPPCORECHECK_WARNINGS)
#endif // _MSC_VER >= 1910
#endif // _MSC_VER


#include <gsl/gsl>


#ifdef _MSC_VER
#if _MSC_VER >= 1910
#pragma warning(default: ALL_CPPCORECHECK_WARNINGS)
#endif // _MSC_VER >= 1910
#endif // _MSC_VER
```

# Code example

I created a code example.
https://github.com/TomoyukiAota/CoreCheckerAndGsl
You should get something like this after the steps described above.


# Afterword

In this article, I described how to introduce C++ Core Guidelines Checker and GSL. I recognize that there is some awkwardness. C++ Core Guidelines Checker works only on VS on Windows. GSL is installed by copy and paste and a wrapper for suppressing warnings are required. However, I believe the warnings from C++ Core Guidelines Checker allows me to write better C++ code and GSL provides better types/functions, so I hope such awkwardness is cleared someday.