return const and assert

A

alexhong2001

When to return a const, from either a member function of a class or
non-member?

Is assert() only executed in debug mode but skipped in release mode? I
don't see much usage of "assert()." Am I missing its importance?

Thanks for your comments!
 
V

Victor Bazarov

alexhong2001 said:
When to return a const, from either a member function of a class or
non-member?

When returning by value, 'const' really isn't of any use. It's
basically like adding 'const' when passing by value: means almost
nothing.
Is assert() only executed in debug mode but skipped in release mode?

Using your terminology, yes.
I
don't see much usage of "assert()." Am I missing its importance?

Probably. 'assert' is a debugging tool for code where you don't
want to have a run-time check once you cleaned everything up.

V
 
P

Phlip

alexhong2001 said:
When to return a const, from either a member function of a class or
non-member?

Call these "top level const":

void function1(int const q);
int const function2();

Don't do that. The 'const' has no meaning to the calling code, because both
values pass by copy. The qualification (const, non-const, volatile,
non-volatile) of the copy are irrelevant.

But do this:

class foo {
int bar;
public:
int const & getBar() { return bar; }
};

Do that so those who access foo::bar cannot change its value. The /Effective
C++/ books cover this.
Is assert() only executed in debug mode but skipped in release mode?

If your implementation's "release mode" defines NDEBUG, then assert() goes
away. But this newsgroup is not qualified to discuss "release mode", because
the configurations that your implementation provides by default are
implementation-specific. Questions about "release mode" will get the best
answers on your compiler's newsgroup.
I don't see much usage of "assert()." Am I missing its importance?

Assert() is the most important function in the whole programming industry.
You won't see it much in the tutorial code. But consider this simple
example:

int main()
{

Source aSource("a b\nc, d");

string
token = aSource.pullNextToken(); assert("a" == token);
token = aSource.pullNextToken(); assert("b" == token);
token = aSource.pullNextToken(); assert("c" == token);
token = aSource.pullNextToken(); assert("d" == token);
token = aSource.pullNextToken(); assert("" == token);
// EOT!
}

Passing the test requires objects of type Source to parse strings, ignoring
spaces and commas. If you add assertions to test cases like this, get them
to fail, and then edit your source to make the tests pass, you can grow
programs of any size and complexity, without the need to ever operate your
debugger. Run the tests after every 1~10 edits, and only perform the kinds
of edits that immediately return a program to a state where all the tests
still pass.

That technique trades long hours debugging for short minutes writing tests.
 
L

lilburne

alexhong2001 said:
I don't see much usage of "assert()." Am I missing its importance?

This is a function to determine the number of steps required to
approximate the portion of a curve, between parametric values t_start
and t_end, to a given tolerance.

int Curve::num_steps(
double t_start,
double t_end,
double eps
) const
{
CHECKVALID_CLASS;
ASSERT_STATE(!invalid(),"Curve::num_steps");
ASSERT_STATE(!infinite(),"Curve::num_steps");
ASSERT_ARGUMENT(eps>0.0,"Curve::num_steps");
ASSERT_ARGUMENT(t_min()<=t_start, "Curve::num_steps");
ASSERT_ARGUMENT(t_end>=t_start, "Curve::num_steps");
ASSERT_ARGUMENT(t_end<=t_max(), "Curve::num_steps");

// compute the number of spans to match given tolerance.
const int nspans = utMath::ceiling((t_end - t_start) *
utMath::sqrt(deriv2_max(t_start,t_end)/(8.0*eps)));
// make sure we have at least one span.
return nspans<1 ? 1 : nspans;
}

As you can see assertions of one form or another dominate the function.
Asserts help you to build reliable systems, detect and zero in on bugs
quickly, and provide the mechanism for writing automatic tests.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top