I have a function like this:

bool MyFunction(dist)
{
	if(dist < 1.2)
	{
		std::cout << "Passed test!" << std::endl;
		return true;
	}
	else
	{
		std::cout << "Failed test!" << std::endl;
		return false;
	}
	std::cout << std::endl;
}

gcc tells me "warning- control reaches end of non-void function". However, the conditional must catch all cases (the else catches everything 'else' (by definition)). So clearly all that will happen is the last endl will never be hit. Is that what it is trying to tell me?

Thanks,

Dave

Recommended Answers

All 3 Replies

Probably so. What happens if you delete or comment out that line?

If that line is remove then there is no warning.

I guess my complaint was just that I thought the warning should say "the function returns before reaching this point" rather than "we have reached the end of a function that is supposed to return something, but it did not return anything".

Dave

Well it is basically telling you that you have an old version of gcc.
(or a very new one that I haven't tested that has changed.)

If you write this:

// GIVES WARNING:
bool MyFunction(const double dist)
{
  if(dist < 1.2)
     return true;
  else if (dist>4.0)
    return false;
}

you get a warning (g++ version: 4.3.2)
Compiled with g++ -c -Wextra -Wall test.cpp BUT this doesn't give a waring

bool MyFunction(const double dist)
{
   if(dist < 1.2)
     return true;
  else 
     return false;
}

Note that the gcc does not do anything fancy to work out that the test has complete coverage (e.g if the second test was dist>1.0).

Give that you have not copied your code fragment from your test program, did you have an "else if" at the end?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.