Should this generate a warning?

S

stewart.tootill

I ran into a code fragment similar to the following in a production
code base - although it was even worse since the class jimlad was a
class for doing locking similar to boost::mutex::scoped_lock.

class jimlad {
public:
jimlad( int a ) { std::cout << "CONSTRUCT JIMLAD" << std::endl; }
~jimlad() { std::cout << "DESTRUCT JIMLAD" << std::endl; }
};

int main(int argc, char* argv[])
{
jimlad(1); //** This line

std::cout << "MIDDLE" << std::endl;

return 0;
}

The output is
CONSTRUCT JIMLAD
DESTRUCT JIMLAD
MIDDLE

I know that the original author intended the marked line to read
"jimlad something(1)" and to produce the output

CONSTRUCT JIMLAD
MIDDLE
DESTRUCT JIMLAd

and I can see why the code is wrong. My question is should it produce
a warning? It doesn't on VC7.1 or VC8, but I don't have any other
compilers handy to try it with, or a copy of the standard.

Can anyone try it on another compiler or let me know why it doesn't
warn you? Unused temporary or something would seem right to me.
 
D

David Harmon

On Tue, 19 Jun 2007 09:17:32 -0700 in comp.lang.c++,
(e-mail address removed) wrote,
Can anyone try it on another compiler or let me know why it doesn't
warn you? Unused temporary or something would seem right to me.

It is the compiler's prerogative to warn you or not warn you about
anything it might feel like (in addition to diagnostics required by the
standard.) Unused temporaries are ubiquitous, for example unused return
values from functions, and a compiler that warned you about every little
routine thing like that might not be very popular.
 

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,292
Messages
2,571,494
Members
48,179
Latest member
รับปั๊มไลค์|LikePro

Latest Threads

Top