Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

My first “goto” since … 1986?

55 views
Skip to first unread message

Alf P. Steinbach

unread,
Jan 10, 2016, 2:37:44 AM1/10/16
to
auto overflow( In_<int_type> char_code = traits_type::eof() )
-> int_type override
{
const Ptr_<const Char> p_after = pptr();
if( p_after != nullptr )
{
const Ptr_<const Char> p_first = pbase();
if( p_after > p_first )
{
String s( p_first, p_after );
if( char_code != traits_type::eof() ) { s << Char(
char_code ); }
console::write( s );
goto finished;
}
}
if( char_code != traits_type::eof() ) { console::write_char(
char_code ); }

finished:
init_put_area_pointers();
// return (success? traits_type::not_eof( char_code ) :
traits_type::eof());
return traits_type::not_eof( char_code );
}

The 1986 "goto" was in COBOL.

If I remember this correctly.


Cheers,

- Alf

Alf P. Steinbach

unread,
Jan 10, 2016, 5:30:54 AM1/10/16
to
On 1/10/2016 8:37 AM, Alf P. Steinbach wrote:
> auto overflow( In_<int_type> char_code = traits_type::eof() )
> -> int_type override
> {
> const Ptr_<const Char> p_after = pptr();
> if( p_after != nullptr )
> {
> const Ptr_<const Char> p_first = pbase();
> if( p_after > p_first )
> {
> String s( p_first, p_after );
> if( char_code != traits_type::eof() ) { s << Char(
> char_code ); }
> console::write( s );
> goto finished;
> }
> }
> if( char_code != traits_type::eof() ) { console::write_char(
> char_code ); }
>
> finished:
> init_put_area_pointers();
> // return (success? traits_type::not_eof( char_code ) :
> traits_type::eof());
> return traits_type::not_eof( char_code );
> }
>

Well OK what else does one have to do on a Sunday's morning than posting
to clc++? By guaranteeing a buffer to write to, the above code was
simplified enough that the “goto” disappeared. This makes the code
slightly less general, but there's not much point in writing potentially
reusable code without some actual reuse visible ahead, so:

auto overflow( In_<int_type> char_code = traits_type::eof() )
-> int_type override
{
const Ptr_<const Char> p_after = pptr();
const Ptr_<const Char> p_first = pbase();
if( p_after > p_first )
{
String s( p_first, p_after );
if( char_code != traits_type::eof() ) { s << Char(
char_code ); }
console::write( s );
}
else if( char_code != traits_type::eof() )
{
console::write_char( char_code );
}

set_put_area_pointers();
// return (success? traits_type::not_eof( char_code ) :
traits_type::eof());
return traits_type::not_eof( char_code );
}

If anyone should wonder, this is an override in a class derived from
std::basic_streambuf<Char>.

The interface is dirty, the naming of things is ungrokable, and
different standard library implementations seem to have different
opinions about how it should work...


Cheers,

- Alf

Mr Flibble

unread,
Jan 10, 2016, 1:10:38 PM1/10/16
to
No reason for that goto whatsoever: multiple return statements in a
function is perfectly fine sausages.

/Flibble

Öö Tiib

unread,
Jan 10, 2016, 2:24:23 PM1/10/16
to
Attempts to have single return in C++ can become rather ugly with
exceptions enabled. I have long dropped such attempts. If function
fits to my monitor then I see all exits clearly (thanks to syntax
highlighting). If it does not then ... that is defect ... one must
be capable of writing shorter functions than 150 lines. Covering
150+ lines function with unit tests is often titanic task anyway.

Mr Flibble

unread,
Jan 10, 2016, 3:08:15 PM1/10/16
to
Enforcing single return statement makes sense in a language like C which
doesn't have constructors, RAII and exceptions but is totally
inappropriate in a language like C++ sausages.

/Flibble
0 new messages