Computer Chess Club Archives


Search

Terms

Messages

Subject: Off-topic: warning elimination

Author: Dave Gomboc

Date: 22:12:04 10/26/99

Go up one level in this thread


On October 26, 1999 at 23:49:26, Dann Corbit wrote:

>Beats me.  Maybe this will help:
>
>http://www7180.nrlssc.navy.mil/usr.local.info.html/gcc.info.Labeled_Elements.html

Nope.

>I always leave warnings on maximum and ignore the compiler when it has a
>brain-fart.
>
>Can you show the code that is causing the problem?  I don't suppose that it is a
>multi-dimentional initialization?


First, an adaptation of Matt Austern's block.  Anyone who wants the speed of C
arrays but the convenience of using C++ standard algorithms with them shouldn't
go without it.  That means all you chess programmers eager to switch to C++! ;-)

--------------------------------------------------------------------------

/*
** block
**
** STL container that encapsulates a C array.
**
** Adapted from code found in Generic Programming and the STL: Using
** and Extending the C++ Standard Template Library, by Matthew Austern.
** ISBN: 0-201-30956-4
*/

template <typename T, size_t N>
struct block {

    typedef T value_type;

    typedef       value_type * pointer;
    typedef const value_type * const_pointer;
    typedef       value_type & reference;
    typedef const value_type & const_reference;

    typedef       ptrdiff_t    difference_type;
    typedef       size_t       size_type;

    typedef       pointer      iterator;
    typedef const pointer      const_iterator;

    iterator       begin() { return data; };
    const_iterator begin() const { return data; };

    iterator       end()   { return data + N; };
    const_iterator end()   const { return data + N; };

    typedef std::reverse_iterator <iterator>       reverse_iterator;
    typedef std::reverse_iterator <const_iterator> const_reverse_iterator;

    reverse_iterator rbegin() { return reverse_iterator (end()); };
    reverse_iterator rend() { return reverse_iterator (begin()); };

    const_reverse_iterator rbegin() const {
          return const_reverse_iterator (end());
    };
    const_reverse_iterator rend() const {
          return const_reverse_iterator (begin());
    };

    reference       operator[] (size_type n) { return data[n]; };
    const_reference operator[] (size_type n) const { return data[n]; };

    size_type size()     const { return N; };
    size_type max_size() const { return N; };
    bool      empty()    const { return 0 == N; };

    void swap (block & x) {
        for (size_type n = 0; n < N; ++n) {
            std::swap (data[n], x.data[n]);
        };
    };

    T data[N];
};

template <typename T, size_t N>
bool operator== (const block <T,N> & x, const block <T,N> & y) {
    return std::equal (x.begin(), x.end(), y.begin());
};

template <typename T, size_t N>
bool operator< (const block <T,N> & x, const block <T,N> & y) {
    return std::lexicographical_compare (x.begin(), x.end(), y.begin());
};


----------------------------------------------------------------------

Okay, so here's the deal.  The C array data, which holds the "real" data, can be
initialized just like you might initialize a C array.

int my_info[5] = { 10, -4, 16, -45, 2 };  /* C-style */

block <int, 5> my_info = { 10, -4, 16, -45, 2 };  // C++-style

g++ is complaining: it wants to see

block <int, 5> my_info = { { 10, -4, 16, -45, 2 } };

because for the block, it's the C-array inside the struct that is being
initialized.  It is, however, perfectly legal to use only one brace (which is
why it's a warning, not an error.)

The latter way looks ugly to me, so I want to use only one { } set, and shut the
warning off.

Dave



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.