473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why does this compile?

Hello,

I was playing around a accidently realized that the following compiles:

//BEGIN CODE
class foo
{
public:
void push(int* iptr) { }
int* peek( )const { }
int* pop( ) { }
};

int main( ) { }
//END CODE

I would expect the compiler to complain because, for example, peek( )
doesn't return an int* or anything else for that matter. If I add
something like 'return 4;' to peek( ) the compiler complains as expected
about the return type mismatch so the empty case seems to be something
special. If anyone could shed some light I'd appreciate it. Thanks in
advance.

-exits

Jul 22 '05 #1
9 2399
On Sun, 18 Jan 2004 23:39:36 GMT,
exits funnel <ex*********@NOSPAMyahoo.com> wrote:
Hello,

I was playing around a accidently realized that the following compiles:

//BEGIN CODE
class foo
{
public:
void push(int* iptr) { }
int* peek( )const { }
int* pop( ) { }
};

int main( ) { }
//END CODE

I would expect the compiler to complain because, for example, peek( )
doesn't return an int* or anything else for that matter. If I add
something like 'return 4;' to peek( ) the compiler complains as expected
about the return type mismatch so the empty case seems to be something
special. If anyone could shed some light I'd appreciate it. Thanks in
advance.


Your compiler sucks.

Or you haven't turned warnings on.

Leaving it off isn't an error according to the language (at least as
far as I recall).

--
Sam Holden
Jul 22 '05 #2

"exits funnel" <ex*********@NOSPAMyahoo.com> wrote in message news:40**************@NOSPAMyahoo.com...
Hello,

I was playing around a accidently realized that the following compiles:

Falling off the end of a non-void returning function is undefined behavior.
The compiler is NOT required to diagnose this. This case is trivial. But
imagine a more complex function:
int* peek() {
while(some_condition()) {
blah();
if(some_other_condition()) return foobar();
}
// do we ever get here;
}
Jul 22 '05 #3
exits funnel wrote:
Hello,

I was playing around a accidently realized that the following
compiles:

//BEGIN CODE
class foo
{
public:
void push(int* iptr) { }
int* peek( )const { }
int* pop( ) { }
};

int main( ) { }
//END CODE

I would expect the compiler to complain because, for example, peek( )
doesn't return an int* or anything else for that matter. If I add
something like 'return 4;' to peek( ) the compiler complains as
expected about the return type mismatch so the empty case seems to be
something
special. If anyone could shed some light I'd appreciate it. Thanks
in advance.


The compiler is not required to issue an error message (though a good
one should at least print a waring if you enable all the diagnostic
options), but returning nothing from a function with non-void return
type invokes undefined behaviour.

Jul 22 '05 #4
Sam Holden wrote:
On Sun, 18 Jan 2004 23:39:36 GMT,
exits funnel <ex*********@NOSPAMyahoo.com> wrote:
Hello,

I was playing around a accidently realized that the following compiles:

//BEGIN CODE
class foo
{
public:
void push(int* iptr) { }
int* peek( )const { }
int* pop( ) { }
};

int main( ) { }
//END CODE

I would expect the compiler to complain because, for example, peek( )
doesn't return an int* or anything else for that matter. If I add
something like 'return 4;' to peek( ) the compiler complains as expected
about the return type mismatch so the empty case seems to be something
special. If anyone could shed some light I'd appreciate it. Thanks in
advance.

Your compiler sucks.

Or you haven't turned warnings on.

Leaving it off isn't an error according to the language (at least as
far as I recall).


It's true that if I turn the warning level up, the compiler does warn me
about missing return statements but the question still remains (assuming
my compiler is compliant with the spec): Why is a return type mismatch
an error while no return statement at all isn't? What's the rationale?

-exits

Jul 22 '05 #5
On Sun, 18 Jan 2004 23:57:24 GMT,
exits funnel <ex*********@NOSPAMyahoo.com> wrote:

It's true that if I turn the warning level up, the compiler does warn me
about missing return statements but the question still remains (assuming
my compiler is compliant with the spec): Why is a return type mismatch
an error while no return statement at all isn't? What's the rationale?


Becsaue a return type mismatch causes a type error, but a missing return
might be intended. There's already a reply giving an example of a case
in which leaving off the return might be valid.

Note: Java requires the return, and the idiot compiler not noticing that
the end of a function was unreachable was enough to drive me mad (I
dislike being forced to add the extra code which will never be executed
and the comment required to warn future readers of why the code was
added...

--
Sam Holden
Jul 22 '05 #6
"exits funnel" <ex*********@NOSPAMyahoo.com> wrote in message
news:40**************@NOSPAMyahoo.com...
Hello,

I was playing around a accidently realized that the following compiles:

//BEGIN CODE
class foo
{
public:
void push(int* iptr) { }
int* peek( )const { }
int* pop( ) { }
};

int main( ) { }
//END CODE

I would expect the compiler to complain because, for example, peek( )
doesn't return an int* or anything else for that matter. If I add
something like 'return 4;' to peek( ) the compiler complains as expected
about the return type mismatch so the empty case seems to be something
special. If anyone could shed some light I'd appreciate it. Thanks in
advance.

-exits


My compiler (VC++.Net) also compiles your code without an error. But if I
try to use one of the bogus functions it gives an error:

foo duh;
int *p = duh.peek();

gives me the error:

error C4716: 'foo::peek' : must return a value

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #7
Sam Holden wrote:
On Sun, 18 Jan 2004 23:57:24 GMT,
exits funnel <ex*********@NOSPAMyahoo.com> wrote:
It's true that if I turn the warning level up, the compiler does warn me
about missing return statements but the question still remains (assuming
my compiler is compliant with the spec): Why is a return type mismatch
an error while no return statement at all isn't? What's the rationale?

Becsaue a return type mismatch causes a type error, but a missing return
might be intended. There's already a reply giving an example of a case
in which leaving off the return might be valid.


I have not seen this reply.
Would you mind restating what is so valid about ignoring the return type
of a function or method that you design?????

Note: Java requires the return, and the idiot compiler not noticing that
the end of a function was unreachable was enough to drive me mad (I
dislike being forced to add the extra code which will never be executed
and the comment required to warn future readers of why the code was
added...


I agree that the compiler should catch those return errors, however, I
fail to understand why ignoring the return type of a function is such a
a great idea.

Jorge L.
Jul 22 '05 #8
Jorge Rivera wrote:
Sam Holden wrote:
On Sun, 18 Jan 2004 23:57:24 GMT,
exits funnel <ex*********@NOSPAMyahoo.com> wrote:
It's true that if I turn the warning level up, the compiler does warn
me about missing return statements but the question still remains
(assuming
my compiler is compliant with the spec): Why is a return type
mismatch
an error while no return statement at all isn't? What's the
rationale?

Becsaue a return type mismatch causes a type error, but a missing
return might be intended. There's already a reply giving an example
of a case in which leaving off the return might be valid.


I have not seen this reply.
Would you mind restating what is so valid about ignoring the return
type of a function or method that you design?????


If your function will never reach a certain place, it's just unecessary
if you need to write a return statement for that unreachable part. For
example:

string foo(const string& s)
{
if (s == "blah")
return do_something();
else if (s == "whatever")
return do_something_else();
else
return some_error;

// this part can never be reached. Why should one be forced to
// add a return statement here?
}

If you add that return, some compilers will even warn you about the fact
that you wrote unreachable code.
Note: Java requires the return, and the idiot compiler not noticing
that the end of a function was unreachable was enough to drive me mad
(I dislike being forced to add the extra code which will never be
executed and the comment required to warn future readers of why the
code was added...


I agree that the compiler should catch those return errors, however, I
fail to understand why ignoring the return type of a function is such
a a great idea.


Ignoring it generally isn't, but in specific situations, it is better
than needing to write unreachable code. In my above example, the
compiler could easily find out that the code is unreachable, but it's
not always so obvious, so a programmer might indeed intend to omit a
return because he knows that it can't ever be reached, or at least
shouldn't ever be reached. In the latter case, it makes more sense to
add an assert instead of a return. The compiler might not be able to
know that this place in the function won't never be reached.

Jul 22 '05 #9
On Fri, 23 Jan 2004 03:34:01 GMT, Jorge Rivera
<jo*****@rochester.rr.com> wrote:
Sam Holden wrote:
On Sun, 18 Jan 2004 23:57:24 GMT,
exits funnel <ex*********@NOSPAMyahoo.com> wrote:
It's true that if I turn the warning level up, the compiler does warn me
about missing return statements but the question still remains (assuming
my compiler is compliant with the spec): Why is a return type mismatch
an error while no return statement at all isn't? What's the rationale?

Becsaue a return type mismatch causes a type error, but a missing return
might be intended. There's already a reply giving an example of a case
in which leaving off the return might be valid.


I have not seen this reply.
Would you mind restating what is so valid about ignoring the return type
of a function or method that you design?????


int f()
{
this_function_is_guaranteed_to_throw();
}

or

int f(T t)
{
switch(t)
{
case 1:
return something;
case 2:
return something_else;
default:
assert(("Should never get here", false));
}
}

Why should the compiler complain and require a bogus return just to
shut it up? The bogus return will probably add unnecessary machine
code to your executable, contributing to code bloat.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
8
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
4
by: Alex Vinokur | last post by:
Compiler GNU gpp.exe (GCC) 3.4.1 Foo(300) = Foo(500); // Foo(300) is const. Why does a compiler compile that? ------ foo.cpp ------ struct Foo { explicit Foo(int) {}
5
by: Genboy | last post by:
My "VIS" Website, which is a C# site created in VS.NET, Framework 1.1, is no longer compiling for me via the command line. As I have done 600 times in the last year and a half, I can compile to...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
13
by: Bob Jones | last post by:
Here is my situation: I have an aspx file stored in a resource file. All of the C# code is written inline via <script runat="server"tags. Let's call this page B. I also have page A that contains...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
11
by: MonkeeSage | last post by:
A quick question about how python parses a file into compiled bytecode. Does it parse the whole file into AST first and then compile the AST, or does it build and compile the AST on the fly as it...
5
by: Jeff | last post by:
hi asp.net 2.0 I get this compile error: 'Image' does not contain a definition for 'ImageUrl' Image image = (Image)e.Item.FindControl("img"); image.ImageUrl = "~/image.png";
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.