It is entirely possible to do this with a macro (just like assert works):
Code:
#include <iostream>
#define ASSERT_DIMENSION(x) \
do \
{ \
if (!x.assert_dimension()) \
{ \
std::cout << "Dimension is not set for " << #x << '\n'; \
} \
} while(0)
class Matrix1
{
public:
Matrix1(int n = 0): d(n) {}
bool assert_dimension() const { return d != 0; }
private:
int d;
};
int main()
{
Matrix1 a(32), b;
ASSERT_DIMENSION(a);
ASSERT_DIMENSION(b);
}
Of course, there's no need to roll your own (probably broken) assert when there's a perfectly good assert (and there's no need for a function dimension_check as the condition to assert is very simple).