Topic: iosfwd and coding style


Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1997/01/02
Raw View
Alessandro Vesely <mc6192@mclink.it> writes:

> I appreciate the templatization effort that will enable us to use strings
> and streams of whatever character type. However, I see a serious problem
> in it. The Standard C++ Library used to be intended not only as a common
> set of useful classes, but also as a guide to programmers, an indication
> of what is to be considered good programming style.

Yes and no.  Much of the library is very low level; the reason for it
being in a library is precisely so that programmers don't have to do
things at such a low level.

> My question is: does anybody seriously think that the following should
> be considered good programming style?
>
>    int_type c = some_get_function();
>    if ( traits_type::eq_int_type( c, traits_type::to_int_type( '?')))
>       // ...

It depends on what you are trying to do.  At the application level, for
example, I cannot think of an occasion where it would occur.

> I mean, why are we are using a language with operators overloading if
> we cannot write
>
>    if ( c == '?')
>       // ...
>
> I know that traits 'only' need to be used when writing templates that
> somehow deal with characters. Unfortunately, the vast majority of classes
> and functions happen to somehow deal with characters. Shouldn't they be
> templatized as well? And what coding style should be recommended?

With regards to characters, IMHO, an application will choose ONE (and
only one) character type for internal use, normally, wstring, I
imagine.  The only case where other character types may appear will be
in the iostream classes; a well designed implementation should be able
to convert on the fly.  Note, for example, that handling multi-byte
codes internally is extremely difficult.

Given this, the application level routines/classes which process strings
will NOT be templates, and will not need to jump through hoops to be
able to handle any strange type thrown at them.  The standard library
goes out of its way to be flexible, at a serious cost in simplicity,
precisely so that the application software won't have to.

BTW: my experience in multi-lingual environments is that at the
application level, you will be almost always comparing (w)string's, and
not some character type, anyway.  And of course, operator== is defined
for basic_string.  (Consider that, e.g.: in German, the two character
sequence 0x73, 0x73 must compare equal to the single character 0xdf.
Locale dependant collating functions will take this into account, but it
obviously means that they cannot simply compare character by character.)

> IMHO, iosfwd reveals either a serious weakness in the predefined types
> or a fundamental flaw in the C++ I/O Library design, depending on the
> point of view.

IMHO, iosfwd is a very good solution to a problem in the language, that
you cannot forward declare a template instantiation as a class.  The
problem in the language is linked to implementation difficulties; while
there are probably solutions, they are not obvious.

> Has anybody made good proposals in this sense? If not, I would urge some
> ideas. I would also like to know what does everybody else think about that.

--
James Kanze         +33 (0)3 88 14 49 00         email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
                            -- Beratung in industrieller Datenverarbeitung
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Alessandro Vesely <mc6192@mclink.it>
Date: 1996/12/30
Raw View
I appreciate the templatization effort that will enable us to use strings
and streams of whatever character type. However, I see a serious problem
in it. The Standard C++ Library used to be intended not only as a common
set of useful classes, but also as a guide to programmers, an indication
of what is to be considered good programming style.

My question is: does anybody seriously think that the following should
be considered good programming style?

   int_type c = some_get_function();
   if ( traits_type::eq_int_type( c, traits_type::to_int_type( '?')))
      // ...

I mean, why are we are using a language with operators overloading if
we cannot write

   if ( c == '?')
      // ...

I know that traits 'only' need to be used when writing templates that
somehow deal with characters. Unfortunately, the vast majority of classes
and functions happen to somehow deal with characters. Shouldn't they be
templatized as well? And what coding style should be recommended?

IMHO, iosfwd reveals either a serious weakness in the predefined types
or a fundamental flaw in the C++ I/O Library design, depending on the
point of view.

Has anybody made good proposals in this sense? If not, I would urge some
ideas. I would also like to know what does everybody else think about that.

Regards and Happy New Year
Ale
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]