Opened 10 years ago

Closed 10 years ago

#8327 closed Support Requests (fixed)

iostream header and std::cout in hpp files must be removed.

Reported by: pjtsu Owned by: John Maddock
Milestone: To Be Determined Component: math
Version: Boost 1.53.0 Severity: Problem
Keywords: Cc:

Description

I think that direct including iostream header and ostream instance like cout, cerr or so must be deprecated.

Reasons

  • to support environment which ostream is forbidden like embedded systems.
  • to entrust output type to library user.

Alternative plan is to replace ostream instance to template-type as below.

  • before
    template <typename Iter>
    void PrintTest(Iter first, Iter last)
    {
            std::cout << std::distance(first, last) << ": ( ";
            for (Iter iter = first; iter != last; ++iter) {
                    std::cout << *iter << " ";
            }
            std::cout << ")" << std::endl;
    }
    
  • after
    template <typename Stream, typename Iter>
    void PrintTest(Stream& ostr, Iter first, Iter last)
    {
            ostr << std::distance(first, last) << ": ( ";
            for (Iter iter = first; iter != last; ++iter) {
                    ostr << *iter << " ";
            }
            ostr << ")" << std::endl;
    }
    
  • Even if ostream header is not included, std::distance() and std::endl are ignored until PrintTest() is substantialized.

Subject

  • boost/algorithm/searching/detail/debugging.hpp
  • boost/graph/graph_utility.hpp
  • boost/math/constants/generate.hpp
  • boost/pending/relaxed_heap.hpp
  • or so ...

Change History (6)

comment:1 by John Maddock, 10 years ago

There are several points here:

1) The compiler is entitled to reject PrintTest if std::endl and std::distance have not been declared even if the function is never instantiated. GCC and Clang are examples that will do this.

2) iostream and associated headers are part of the standard, it's not unreasonable to reply on them.

3) The only Math lib header mentioned above is only ever used when generating new numeric constants so should never be an issue in practice.

There is an include of <iostream> in boost/math/tools/precision.hpp which isn't needed which I'll remove.

And finally, please file one bug report per library.

Last edited 10 years ago by John Maddock (previous) (diff)

in reply to:  1 comment:2 by pjtsu, 10 years ago

Replying to johnmaddock:

Thank you for your reply.

1) The compiler is entitled to reject PrintTest if std::endl and std::distance have not been declared even if the function is never instantiated. GCC and Clang are examples that will do this.

Sorry, I did not check on GCC.

2) iostream and associated headers are part of the standard, it's not unreasonable to reply on them.

Windows GUI application can build with cout, but the application don't have I/O console.
Therefore ostringstream, ofstream or user-customized stream like may be used alternativelly.
If stream argument is not ostream but template-type, independent class from ostream can be used. (as like CDumpContext in MFC. ... though std::endl must be removed.)

// not so great
template <typename Iter>
void PrintTest(std::ostream& ostr, Iter first, Iter last);

// good
template <typename Stream, typename Iter>
void PrintTest(Stream& ostr, Iter first, Iter last);


3) The only Math lib header mentioned above is only ever used when generating new numeric constants so should never be an issue in practice.

There is an include of <iostream> in boost/math/tools/precision.hpp which isn't needed which I'll remove.

And finally, please file one bug report per library.

All right.

comment:3 by pjtsu, 10 years ago

Incidentally, override of std::endl and std::flush for CDumpContext in MFC is as below. (I checked only vs2010.)

inline CDumpContext& operator << (CDumpContext& me, CDumpContext & (*_f)(CDumpContext&)) { return (*_f)(me); }
namespace std {
        inline CDumpContext& flush(CDumpContext& me) { me.Flush(); return me; }
        inline CDumpContext& endl(CDumpContext& me) { return me << _T("\n") << flush; }
};

comment:4 by John Maddock, 10 years ago

class Stream and CDumpContext are entirely specific to MSVC, they won't appear anywhere in Boost code.

Also note that std::iostream code is only used in Boost.Math for debugging/diagnostic purposes (when BOOST_MATH_INSTRUMENT is defined). What is used is boost::lexical_cast which depends on std::stringstream and related code. Removing that is much harder - verging on impossible if we want to retain multiprecision support - which we certainly do.

comment:5 by John Maddock, 10 years ago

(In [83532]) Reorganise header inclusion to minimise dependencies on <iostream> and <boost/lexical_cast.hpp>. Refs #8327.

comment:6 by John Maddock, 10 years ago

Resolution: fixed
Status: newclosed

(In [83537]) Make use of lexical_cast (and hence multiprecision support) dependent on there being an iostreams lib. Should allow use on embedded platforms, especially WinCE. Fixes #8327.

Note: See TracTickets for help on using tickets.