Re: How should I specify size?

From: Jack Klein (jackklein_at_spamcop.net)
Date: 02/03/04


Date: Tue, 03 Feb 2004 04:49:41 GMT

On Mon, 02 Feb 2004 20:02:02 GMT, "Mike Wahler"
<mkwahler@mkwahler.net> wrote in comp.lang.c++:

>
> "William Payne" <mikas493_no_s_p_a_m_@student.liu.se> wrote in message
> news:bvm97i$443$1@news.island.liu.se...
> > Hello, when using cin.getline() with a char array, how should I specify
> the
> > size (the second parameter of getline)?
> >
> > const int buffer_size = 256;
> > char buffer[buffer_size];
> >
> > std::cin.getline(buffer, std::streamsize(buffer_size));
> > or
> > std::cin.getline(buffer, buffer_size);
> > ?
> >
> > I know, I should use std::string and I usually do, but I still want to
> know
> > the proper way. I am writing a program that will be probably converted to
> C
> > so I ended up with char arrays instead of std::string, and the program
> uses
> > a third-party api which is based on C.
>
> The C++ standard shows these prototypes:
>
> basic_istream<charT,traits>& getline(char_type* s, streamsize n);
> basic_istream<charT,traits>& getline(char_type* s, streamsize n,
> char_type delim);
>
> so I'd use 'streamsize' type.
>
> But with C, you don't have that type, use 'size_t' to specify
> object sizes or counts. And when calling library functions,
> simply use whatever type the prototype specifies.
>
>
> -Mike

I disagree here, Mike. Since C++ requires a proper prototype in
scope, the conversion is automatic. The functional-like cast just
adds unnecessary clutter in this case.

My rule is never provide an explicit cast for an implicit automatic
loss-less conversion unless the circumstances are complex enough that
someone reading the code might be mislead.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html 


Relevant Pages

  • Re: Importing Data into Access from Word / txt files
    ... specify the column type for the checkbox as YesNo ... The field label is there, ... I'm making the conversion through Access code. ...
    (microsoft.public.word.conversions)
  • Re: Exceeds maximum index number
    ... and was specifically talking about conversion from an EARLIER version of ... I was looking around, and I think I saw someplace that if you ... specify "enforce referential integrity" for a relationship that a index will be ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Isolate lpar to resolver process
    ... I want to avoid any name to address conversion. ... In your next step you indicate having set up a RESOLVER SETUP ... BPXPRMxx member RESOLVER_PROC statement can specify RESOLVER. ... DEFAULTIPNODES data set with nothing in it - until you decide you might ...
    (bit.listserv.ibm-main)
  • Converting Pipe Delimited Text File To Tab Delimited Text file
    ... All fields must be trimmed of extra spaces (I assume either the TRIM, LTRIM ... OR RTRIM commands can be used for this. ... A form to specify the input and ... output and a button to run the conversion is required for this task. ...
    (microsoft.public.access.modulesdaovba)
  • Re: How should I specify size?
    ... And when calling library functions, ... The functional-like cast just ...
    (comp.lang.cpp)