'include <cassert>

It the header file #include <cassert> still valid in modern c++?

I know TDD users assert, but this is a different use. Has c++ moved on and if so what is the alternative?

I know if I am passing an object as an argument it can manage its own data validity. So I guess the question boils down to is assert still used in relation to built in types, pointers etc?



There is a <cassert>, but you still use assert() and not std::assert() as you'd otherwise expect because it's pulled in from a new header file.

assert() hasn't changed, it takes any expression that evaluates to an int, and is controlled a build time using NDEBUG.
Last edited on
<cassert> is still available in C++. I can't speak for others, but I find it useful in the debug phase of coding when I have a condition that I think is illogical but I want to check anyway.

From https://cplusplus.com/reference/cassert/assert/

This macro is disabled if, at the moment of including <cassert>, a macro with the name NDEBUG has already been defined. This allows for a coder to include as many assert calls as needed in a source code while debugging the program and then disable all of them for the production version by simply including a line like:
 
#define NDEBUG  

at the beginning of the code, before the inclusion of <cassert>.

Therefore, this macro is designed to capture programming errors, not user or run-time errors, since it is generally disabled after a program exits its debugging phase.


It the header file #include <cassert> still valid in modern c++?

Yes.

Has c++ moved on and if so what is the alternative?

I guess contracts would have been an alternative. We almost got them in C++20, but then they started rethinking the whole thing, and now I haven't heard much about them at all recently.

I know if I am passing an object as an argument it can manage its own data validity. So I guess the question boils down to is assert still used in relation to built in types, pointers etc?

assert can be used for any type of data. Built-in or user-defined.
Last edited on
Are you wanting run-time assertion testing, or maybe compile-time testing?

No header needed, C++11's static_assert() might fit the bill if the latter:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// this example will not compile!

template <typename T>
class EverythingButInt
{
public:
   EverythingButInt()
   {
      static_assert(sizeof(T) != sizeof(int), "No int please!");
   }
};

int main()
{
   EverythingButInt<short> aShort;

   EverythingButInt<int> anInt;
}
Thank you all who replied.

Peter87 wrote:
I guess contracts would have been an alternative. We almost got them in C++20, but then they started rethinking the whole thing, and now I haven't heard much about them at all recently.

Doesn't Boost have a form of "contracts?" Apparently yes, though if it is similar to the C++ ISO proposal I don't know.

https://www.boost.org/doc/libs/1_79_0/libs/contract/doc/html/index.html
Topic archived. No new replies allowed.