Subject: [Boost-bugs] [Boost C++ Libraries] #6160: support for (istream >> array < char >)
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-11-22 00:38:37
#6160: support for (istream >> array < char >)
-----------------------------------+----------------------------------------
Reporter: giecrilj@⦠| Owner: marshall
Type: Feature Requests | Status: new
Milestone: To Be Determined | Component: array
Version: Boost 1.44.0 | Severity: Cosmetic
Keywords: |
-----------------------------------+----------------------------------------
1. `array < T >` is a replacement for `T []`
1. the standard library provides the syntax `(istream >> char [])` and
`(ostream << char const [])`
1. Currently, `(istream >> array < char >)` does not mean anything
1. this functionality cannot be simulated even with `std:: copy_n <
istreambuf_iterator >` without manual termination and much verbosity
== My implementation ==
{{{
#!cpp
#include <boost/array.hpp> /* for ::boost:: array */
#include <boost/version.hpp>
#if +BOOST_VERSION <= 0313720
#include <iomanip> /* for ::std:: setw */
namespace boost
{
/* helper classes to prevent ambiguity in matching array < char > */
namespace detail_
{
/* normally, other char for every character type is char */
template < class P_C > class other_char { public: typedef char type; };
/* but other_char is undefined for char */
template <> class other_char < char > {};
/* class same_stream fails for istream */
template
< class P_S,
class P_C = typename other_char < typename P_S:: char_type >:: type >
class same_stream { public: typedef P_S stream; }; }
/* template input */
template < class P_C, class P_T, ::std:: size_t P_N >
static inline ::std:: basic_istream < P_C, P_T >
&operator >>
(::std:: basic_istream < P_C, P_T > &p_s, ::boost:: array < P_C, P_N >
&p_a)
{ return p_s >> ::std:: setw (p_a. static_size) >> p_a. data (); }
/* character input, disabled for type char to avoid ambiguity */
template < class P_C, class P_T, ::std:: size_t P_N >
static inline typename
detail_:: same_stream
< ::std:: basic_istream < P_C, P_T > >:: stream
&operator >>
(::std:: basic_istream < P_C, P_T > &p_s,
::boost:: array < char, P_N > &p_a)
{ return p_s >> ::std:: setw (p_a. static_size) >> p_a. data (); }
/* template output */
template < class P_C, class P_T, ::std:: size_t P_N >
static inline ::std:: basic_ostream < P_C, P_T >
&operator <<
(::std:: basic_ostream < P_C, P_T > &p_s,
::boost:: array < P_C, P_N > const &p_a) { return p_s << p_a. begin (); }
/* character output, disabled for type char */
template < class P_C, class P_T, ::std:: size_t P_N >
static inline
typename detail_:: same_stream < ::std:: basic_ostream < P_C, P_T > >::
stream
&operator <<
(::std:: basic_ostream < P_C, P_T > &p_s,
::boost:: array < char, P_N > const &p_a)
{ return p_s << p_a. begin (); }}
#endif /* BOOST_VERSION */
#include <cstdlib> /* for EXIT_SUCCESS */
#include <cstdio> /* for BUFSIZ */
#include <iostream> /* for ::std:: cin */
int main ()
{ // char (&x) [+BOOST_VERSION] = 0;
#ifdef ARRAY_IN_NATIVE
/* native code */
char t [+BUFSIZ];
return
::std:: cin >> ::std:: setw (+BUFSIZ) >> t && ::std:: cout << t <<
'\n'?
+EXIT_SUCCESS: +EXIT_FAILURE;
#else /* ARRAY_IN_NATIVE */
/* equivalent Boost code */
::boost:: array < char, +BUFSIZ > t;
/* check that character input compiles for wchar_t */
(void) sizeof (std:: wcin >> t);
return
::std:: cin >> t && ::std:: cout << t << '\n'? +EXIT_SUCCESS:
+EXIT_FAILURE;
#endif /* ARRAY_IN_NATIVE */
}
}}}
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/6160> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:07 UTC