Re: Passing Two-Dimensional Array as a Function Parameter

Discussion in 'C++' started by Luc Danton, Oct 3, 2010.

  1. Luc Danton

    Luc Danton Guest

    On 03/10/2010 17:56, Pete Becker wrote:
    > On 2010-10-03 11:49:15 -0400, Pete Becker said:
    >
    >> On 2010-10-03 10:09:41 -0400, Luc Danton said:
    >>
    >>> On 03/10/2010 15:42, Pete Becker wrote:
    >>>> On 2010-10-03 03:31:50 -0400, Juha Nieminen said:
    >>>>
    >>>>> Pete Becker <> wrote:
    >>>>>>> int valsAcross [5] = {0,0,0,0,0};
    >>>>>>
    >>>>>> Or, if you don't like counting all those zeros,
    >>>>>>
    >>>>>> int valsAcross[5] = { 0 };
    >>>>>
    >>>>> I think this will work too:
    >>>>>
    >>>>> int valsAcross[5] = { };
    >>>>
    >>>> Maybe. But mine is much clearer. <g>
    >>>>
    >>>
    >>> Do you find:
    >>>
    >>> template<typename T>
    >>> T make()
    >>> {
    >>> return T();
    >>> }
    >>>
    >>> unclear ?

    >>
    >> No.
    >>
    >>> What is the result of make<int>() ?
    >>>
    >>> What about:
    >>>
    >>> template<typename T>
    >>> T*
    >>> make()
    >>> {
    >>> return new T[10]();
    >>> }
    >>>
    >>> ?
    >>> What is make<int>()[0] ?

    >>
    >> I'm not at all clear what your point is. Yes, contructors are
    >> meaningful, and some people like to wrap them in template functions.
    >>
    >>>
    >>> To me
    >>> int valsAcross[5] = {};
    >>> is just as clear as
    >>> int valsAcross[5] = { 0 };

    >>
    >> Good for you.
    >>
    >>>
    >>> Then again I've seen presentations/read things about C++0x and
    >>> value-initialization.

    >>
    >> Once C++0x becomes widely adopted (the standard is still a year or
    >> more away from finalization) things may change. Until then, code that
    >> uses C++0x language features is certainly not portable, and probably
    >> incomprehensible to many people.

    >
    > However, please note that aggregate initialization of arrays is not a
    > new C++0x feature. It's been around since the early days of C. And while
    > empty braces are allowed as an aggregate initializer, I've always found
    > putting at least one value in the initializer to be clearer.
    >
    > --
    > Pete
    > Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    > Standard C++ Library Extensions: a Tutorial and Reference
    > (www.petebecker.com/tr1book)
    >


    Arguably there are no constructor involved in int(), but you probably
    know that (and it's irrelevant). My point was that there is an effort to
    make initialization uniform no matter the type.

    template<typename T>
    T make() { return T(); }

    is currently (C++03) designed to Do The Right Thing for a large number
    of cases.

    With C++0x,

    template<typename T>
    T make() { return T {}; }

    is designed to take care of even more cases, notably, as you pointed
    out, by pulling aggregate initialization in. Yes, C++0x is not yet here.
    But the snippet already works for C++03. I don't understand your claim
    that putting one value in the initializer makes it clearer, but I of
    course respect that opinion. Have you however considered:

    int array[5] = { 42 };
    ?

    I'd write it in a heartbeat if I had the need rather than the more explicit:
    int array[5] = {}; // or { 0 };
    array[0] = 42;
    but I wouldn't claim that's it's clear!

    Did you really expect that an unconditional
    > Maybe. But mine is much clearer.

    would not generate reactions? Was it in jest?
    Luc Danton, Oct 3, 2010
    #1
    1. Advertising

  2. Luc Danton

    Kai-Uwe Bux Guest

    Luc Danton wrote:

    > On 2010-10-03 11:49:15 -0400, Pete Becker said:

    [...]
    > Did you really expect that an unconditional
    > > Maybe. But mine is much clearer.

    > would not generate reactions? Was it in jest?


    It wasn't all the "unconditional". The original line was:

    Maybe. But mine is much clearer. <g>

    Did you miss the "<g>" ?


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 3, 2010
    #2
    1. Advertising

  3. Luc Danton

    Luc Danton Guest

    On 03/10/2010 20:10, Pete Becker wrote:
    > On 2010-10-03 12:41:22 -0400, Luc Danton said:
    > [ snip }
    > Do you know what "<g>" means?


    Up to now, I absolutely did not!
    Luc Danton, Oct 4, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    423
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,914
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    643
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    963
    Venkat
    Dec 5, 2003
  5. Kai-Uwe Bux
    Replies:
    2
    Views:
    314
    Luc Danton
    Oct 3, 2010
Loading...

Share This Page