I'm starting a project using C++, which I haven't used before outside of a handful of school projects - nowhere near the scope of what I'm tackling now.

My goal is to try my best to follow the C++ Core Guidelines as I work to avoid errors, improve performance, and most importantly: improve maintainability of my code.

I've been running into literally hundreds of issues ranging from my g++ / Clang++ versions not being right to standard libraries not being found to g++ using the wrong version of C++ for compilation to very basic functions not behaving as expected - and I haven't even started to look into autotools, so I expect many more headaches to follow.

This question is specific to one part of the C++ Core Guidelines, though. Interfaces 6: Prefer Expects() for expressing preconditions

I tried writing the following simple code:

#include <iostream>

using namespace std;

int square(int x) {
    Expects(x > 0);
    return x * x;

int main() {
    cout << square(3) << endl;
    return 0;

This threw an error in g++:

$> g++ -std=c++17 main.cpp
main.cpp: In function ‘int square(int)’:
main.cpp:7:2: error: ‘Expects’ was not declared in this scope
  Expects(x > 0);
-> [1]

I tried using Clang, as well, but it has an entirely different (and unrelated) problem:

$> clang++ -x c++ main.cpp
main.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
1 error generated.
-> [1]

I haven't figured out how to fix that one yet, so I'm not bothering with it.

  • 15,753
  • 22
  • 105
  • 182
  • @Galk No such file or directory. Do I need to `sudo apt-get install` something to get the GSL stuff? – stevendesu Feb 04 '19 at 17:15
  • Unrelated to your question but a more modern build generator might be a good idea. I prefer Meson but CMake is a defacto standard. Either would help with your iostream issue as they will find all the normal stuff. – Michael Surette Feb 04 '19 at 22:40
  • I'm actually reading up on CMake right now. I definitely need a better build system. I'm pretty new to C++ development so I'm re-learning everything I spent years figuring out with JavaScript and Python (what build tools exist, what dependency managers exist, how people structure projects, etc) – stevendesu Feb 05 '19 at 15:58

2 Answers2


Expects is part of the GSL library. You have to use some GSL library implementation, which you can find on Github:

These are the ones I have off the top of my head.

The CPP Guidelines likely allude to the "contracts" proposal which provides the same checks via attributes. It was scheduled for C++20, but later removed for lack of consensus on its scope. See p1823r0 and a standard committee member's Reddit thread on the rationale leading to the removal.

Mário Feroldi
  • 3,463
  • 2
  • 24
  • 49

Apart from GSL, Excepts exists also in C++20 not in C++17 with a little different syntax

  • 61
  • 5
  • How is `C++20` a thing? I thought the last two digits referred to the year the version was released (e.g. `C++14` came out in 2014, `C++17` came out in 2017, etc) -- wouldn't this mean that `C++20` came out in 2020? It's only 2019. Am I misunderstanding the naming convention? Also, how would I install and use `C++20`? When I try `g++ -std=c++20` it gives the error `did you mean -std=c++03?` – stevendesu Feb 04 '19 at 17:34
  • 5
    C++20 is still in phase of standardization. They probably meant C++2a, which is a placeholder for the incoming C++20. – Mário Feroldi Feb 04 '19 at 17:40
  • Unfortunately `g++ -std=c++2a` gives me the same error :( I guess I'll just stick to `C++17` with the GSL for now – stevendesu Feb 04 '19 at 17:47
  • 1
    @MárioFeroldi: Nobody calls it "C++2a" except for compiler switches, and only then to express that it isn't a complete C++20 implementation yet. – Nicol Bolas Feb 04 '19 at 18:33
  • 3
    The proposal has been dropped off C++ 2020, see https://www.reddit.com/r/cpp/comments/cmk7ek/what_happened_to_c20_contracts/ – Jules Sam. Randolph Dec 08 '21 at 13:40
  • @stevendesu starting in 2011 a new revision of c++ will come out every 3 years. In the process to the next revision there is a lot of discussion and experimenting before the revision gets finalized by all participants https://isocpp.org agreeing on how to implement specific features. "Contracts" is such a moving part which is experimented on for over a decade now. – Marco Kinski Jun 12 '23 at 13:19