How to use C++ Expects operator?
Asked Answered
S

3

17

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.

Sperling answered 4/2, 2019 at 17:11 Comment(3)
@Galk No such file or directory. Do I need to sudo apt-get install something to get the GSL stuff?Sperling
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.Cassino
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)Sperling
B
15

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.

Belcher answered 4/2, 2019 at 17:18 Comment(3)
It's very odd to read "guidelines" about how to use C++ well and find nonstandard recommendations.Felike
Note that the link is now https://github.com/microsoft/GSL/blob/main/include/gsl/assert. gsl/gsl_assert is deprecated.Collectanea
@RobertBernstein thanks. I've removed the suggestion as to avoid confusion.Plasia
M
3

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

Mummery answered 4/2, 2019 at 17:28 Comment(6)
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?Sperling
C++20 is still in phase of standardization. They probably meant C++2a, which is a placeholder for the incoming C++20.Plasia
Unfortunately g++ -std=c++2a gives me the same error :( I guess I'll just stick to C++17 with the GSL for nowSperling
@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.Glassful
The proposal has been dropped off C++ 2020, see reddit.com/r/cpp/comments/cmk7ek/what_happened_to_c20_contractsImminent
@Sperling 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 isocpp.org agreeing on how to implement specific features. "Contracts" is such a moving part which is experimented on for over a decade now.Frump
A
1

So to answer your actual question:

Your first error is because you did not include "include/gsl/assert" as retrieved from https://github.com/microsoft/GSL. You can copy the entire gsl into your source tree or even submodule it in git. It is header only and doesn't require integration into a build system unless you want to add -I "include/gsl".

Your second error has to do with clang pointing at the wrong directories. and is a sperate issue entirely from the GSL Expects() not working.

Arnitaarno answered 2/7 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.