reference trouble in double[3] vs. double*

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jacek Dziedzic

    reference trouble in double[3] vs. double*

    Hi!

    Let's say I have a class called Triplet that serves as an envelope
    for double[3], ie.

    class Triplet {
    public:
    Triplet() {/*...*/}
    /*
    some things that double[3] doesn't have, like
    a << operator to send it to a stream
    */

    private:
    double storage[3];
    };

    I'm having a problem with the subscript operator, I tried

    double& Triplet::operat or[](const unsigned int i) const {
    return storage[i];
    }

    and was quite surprised to see it won't compile. It works
    fine, however, when I change
    "double storage[3]" to "double *storage" and allocate it
    accordingly.

    I guess I'm being bitten by the differences between array
    of double and pointer to double when it comes to references,
    but can someone shed some light on why what I attempted is
    not possible? I got away with something like

    return (static_cast<do uble*>(&(storag e[0])))[i];

    while also making the operator[] non-const, but it sure looks ugly.

    So... what is it with the array that doesn't allow what
    I'm trying to do? Is there a cleaner way (changing the array
    to a pointer and doing new[] will be overkill, I use these
    Triplets in huge matrices up to 8100x8100).
  • John Harrison

    #2
    Re: reference trouble in double[3] vs. double*


    "Jacek Dziedzic" <jacek__NOSPAM_ _@janowo.net> wrote in message
    news:c51its$551 $1@korweta.task .gda.pl...[color=blue]
    > Hi!
    >
    > Let's say I have a class called Triplet that serves as an envelope
    > for double[3], ie.
    >
    > class Triplet {
    > public:
    > Triplet() {/*...*/}
    > /*
    > some things that double[3] doesn't have, like
    > a << operator to send it to a stream
    > */
    >
    > private:
    > double storage[3];
    > };
    >
    > I'm having a problem with the subscript operator, I tried
    >
    > double& Triplet::operat or[](const unsigned int i) const {
    > return storage[i];
    > }
    >
    > and was quite surprised to see it won't compile. It works
    > fine, however, when I change
    > "double storage[3]" to "double *storage" and allocate it
    > accordingly.[/color]

    Think about it

    const Triplet x;
    x[1] = 2.0;

    do you really want that to compile?
    [color=blue]
    >
    > I guess I'm being bitten by the differences between array
    > of double and pointer to double when it comes to references,
    > but can someone shed some light on why what I attempted is
    > not possible? I got away with something like
    >
    > return (static_cast<do uble*>(&(storag e[0])))[i];
    >
    > while also making the operator[] non-const, but it sure looks ugly.
    >
    > So... what is it with the array that doesn't allow what
    > I'm trying to do?[/color]

    Its nothing to do with arrays, returning a non-const reference to any data
    member from a const method will not compile.
    [color=blue]
    > Is there a cleaner way (changing the array
    > to a pointer and doing new[] will be overkill, I use these
    > Triplets in huge matrices up to 8100x8100).[/color]

    Do it like this

    double& Triplet::operat or[](const unsigned int i) {
    return storage[i];
    }

    double Triplet::operat or[](const unsigned int i) const {
    return storage[i];
    }

    i.e. define const and non-const versions of your operator[].

    john


    Comment

    • John Harrison

      #3
      Re: reference trouble in double[3] vs. double*


      "Jacek Dziedzic" <jacek__NOSPAM_ _@janowo.net> wrote in message
      news:c51its$551 $1@korweta.task .gda.pl...[color=blue]
      > Hi!
      >
      > Let's say I have a class called Triplet that serves as an envelope
      > for double[3], ie.
      >
      > class Triplet {
      > public:
      > Triplet() {/*...*/}
      > /*
      > some things that double[3] doesn't have, like
      > a << operator to send it to a stream
      > */
      >
      > private:
      > double storage[3];
      > };
      >
      > I'm having a problem with the subscript operator, I tried
      >
      > double& Triplet::operat or[](const unsigned int i) const {
      > return storage[i];
      > }
      >
      > and was quite surprised to see it won't compile. It works
      > fine, however, when I change
      > "double storage[3]" to "double *storage" and allocate it
      > accordingly.[/color]

      Think about it

      const Triplet x;
      x[1] = 2.0;

      do you really want that to compile?
      [color=blue]
      >
      > I guess I'm being bitten by the differences between array
      > of double and pointer to double when it comes to references,
      > but can someone shed some light on why what I attempted is
      > not possible? I got away with something like
      >
      > return (static_cast<do uble*>(&(storag e[0])))[i];
      >
      > while also making the operator[] non-const, but it sure looks ugly.
      >
      > So... what is it with the array that doesn't allow what
      > I'm trying to do?[/color]

      Its nothing to do with arrays, returning a non-const reference to any data
      member from a const method will not compile.
      [color=blue]
      > Is there a cleaner way (changing the array
      > to a pointer and doing new[] will be overkill, I use these
      > Triplets in huge matrices up to 8100x8100).[/color]

      Do it like this

      double& Triplet::operat or[](const unsigned int i) {
      return storage[i];
      }

      double Triplet::operat or[](const unsigned int i) const {
      return storage[i];
      }

      i.e. define const and non-const versions of your operator[].

      john


      Comment

      • E. Robert Tisdale

        #4
        Re: reference trouble in double[3] vs. double*

        John Harrison wrote:
        [color=blue]
        > Do it like this
        >
        > double& Triplet::operat or[](const unsigned int i) {
        > return storage[i];
        > }
        >
        > double Triplet::operat or[](const unsigned int i) const {
        > return storage[i];
        > }
        >
        > i.e. define const and non-const versions of your operator[].[/color]

        Or

        const
        double& Triplet::operat or[](const unsigned int i) const {
        return storage[i];
        }

        Comment

        • E. Robert Tisdale

          #5
          Re: reference trouble in double[3] vs. double*

          John Harrison wrote:
          [color=blue]
          > Do it like this
          >
          > double& Triplet::operat or[](const unsigned int i) {
          > return storage[i];
          > }
          >
          > double Triplet::operat or[](const unsigned int i) const {
          > return storage[i];
          > }
          >
          > i.e. define const and non-const versions of your operator[].[/color]

          Or

          const
          double& Triplet::operat or[](const unsigned int i) const {
          return storage[i];
          }

          Comment

          • Ali Cehreli

            #6
            Re: reference trouble in double[3] vs. double*

            Jacek Dziedzic <jacek__NOSPAM_ _@janowo.net> wrote in message news:<c51its$55 1$1@korweta.tas k.gda.pl>...[color=blue]
            > Hi!
            >
            > Let's say I have a class called Triplet that serves as an envelope
            > for double[3], ie.
            >
            > class Triplet {
            > public:
            > Triplet() {/*...*/}
            > /*
            > some things that double[3] doesn't have, like
            > a << operator to send it to a stream
            > */[/color]

            You should have a declaration in the commented-out section like this:

            double const & operator[] (unsigned int i) const;

            Please note that a const member function cannot return a non-const
            reference. For that reason, you must declare the return type as
            'double' or 'double const &'.

            Also note that the 'const' for the argument is not the part of the
            signature but an implementation detail, because the 'i' that users
            pass will be copied to the function.

            For that reason, some argue that it shouldn't take part in the
            interface. It doesn't matter really because that top-level const
            doesn't take part in the signature of the function.

            If you need to provide non-const access too, then you can define the
            non-const version:

            double & operator[] (unsigned int i);
            [color=blue]
            >
            > private:
            > double storage[3];
            > };
            >
            > I'm having a problem with the subscript operator, I tried
            >
            > double& Triplet::operat or[](const unsigned int i) const {
            > return storage[i];
            > }[/color]

            To match the declaration above, the return type must be 'double const
            &' here. Cont-qualifying 'i' is ok here because this is the
            implementation.

            Ali

            Comment

            • Ali Cehreli

              #7
              Re: reference trouble in double[3] vs. double*

              Jacek Dziedzic <jacek__NOSPAM_ _@janowo.net> wrote in message news:<c51its$55 1$1@korweta.tas k.gda.pl>...[color=blue]
              > Hi!
              >
              > Let's say I have a class called Triplet that serves as an envelope
              > for double[3], ie.
              >
              > class Triplet {
              > public:
              > Triplet() {/*...*/}
              > /*
              > some things that double[3] doesn't have, like
              > a << operator to send it to a stream
              > */[/color]

              You should have a declaration in the commented-out section like this:

              double const & operator[] (unsigned int i) const;

              Please note that a const member function cannot return a non-const
              reference. For that reason, you must declare the return type as
              'double' or 'double const &'.

              Also note that the 'const' for the argument is not the part of the
              signature but an implementation detail, because the 'i' that users
              pass will be copied to the function.

              For that reason, some argue that it shouldn't take part in the
              interface. It doesn't matter really because that top-level const
              doesn't take part in the signature of the function.

              If you need to provide non-const access too, then you can define the
              non-const version:

              double & operator[] (unsigned int i);
              [color=blue]
              >
              > private:
              > double storage[3];
              > };
              >
              > I'm having a problem with the subscript operator, I tried
              >
              > double& Triplet::operat or[](const unsigned int i) const {
              > return storage[i];
              > }[/color]

              To match the declaration above, the return type must be 'double const
              &' here. Cont-qualifying 'i' is ok here because this is the
              implementation.

              Ali

              Comment

              • red floyd

                #8
                Re: reference trouble in double[3] vs. double*

                Jacek Dziedzic <jacek__NOSPAM_ _@janowo.net> wrote in message news:<c51its$55 1$1@korweta.tas k.gda.pl>...[color=blue]
                > Hi![/color]
                [redacted]

                Try:

                class Triplet {
                //...
                double& operator[](unsigned int i) { return storage[i]; }
                double operator[](unsigned int i) const { return storage[i]; }
                //...
                };

                Note the different return types for const vs. non-const.

                Comment

                • red floyd

                  #9
                  Re: reference trouble in double[3] vs. double*

                  Jacek Dziedzic <jacek__NOSPAM_ _@janowo.net> wrote in message news:<c51its$55 1$1@korweta.tas k.gda.pl>...[color=blue]
                  > Hi![/color]
                  [redacted]

                  Try:

                  class Triplet {
                  //...
                  double& operator[](unsigned int i) { return storage[i]; }
                  double operator[](unsigned int i) const { return storage[i]; }
                  //...
                  };

                  Note the different return types for const vs. non-const.

                  Comment

                  • Old Wolf

                    #10
                    Re: reference trouble in double[3] vs. double*

                    Jacek Dziedzic <jacek__NOSPAM_ _@janowo.net> wrote:[color=blue]
                    >
                    > class Triplet {
                    > public:
                    > Triplet() {/*...*/}[/color]

                    double& operator[](const unsigned int i) const
                    { return storage[i]; }[color=blue]
                    >
                    > private:
                    > double storage[3];
                    > };
                    >
                    > I'm having a problem with the subscript operator, I
                    > was quite surprised to see it won't compile. It works
                    > fine, however, when I change "double storage[3]" to
                    > "double *storage" and allocate it accordingly.[/color]

                    If a Triplet is const, then its members are const. So the original
                    storage is const and you cannot return a non-const reference to it.
                    But in the case of "double *storage", "storage" is still const but
                    the things it points to are non-const.

                    BCC and GCC 2 give a useful error message for the above code:
                    In method `double & Triplet::operat or [](unsigned int) const':
                    warning: conversion from `const double' to `double &' discards const
                    but GCC 3's output was obfuscated:
                    In member function `double& Triplet::operat or[](unsigned int) const':
                    error: could not convert `this->Triplet::stora ge[i]' to `double&'

                    One solution is to make two operator[] functions, one being const and
                    returning const ref, and the other non-const and returning non-const ref.
                    This is a bit inelegant (especially if you want to support volatile
                    triplets too), I don't know if there is a better solution.

                    Comment

                    • Old Wolf

                      #11
                      Re: reference trouble in double[3] vs. double*

                      Jacek Dziedzic <jacek__NOSPAM_ _@janowo.net> wrote:[color=blue]
                      >
                      > class Triplet {
                      > public:
                      > Triplet() {/*...*/}[/color]

                      double& operator[](const unsigned int i) const
                      { return storage[i]; }[color=blue]
                      >
                      > private:
                      > double storage[3];
                      > };
                      >
                      > I'm having a problem with the subscript operator, I
                      > was quite surprised to see it won't compile. It works
                      > fine, however, when I change "double storage[3]" to
                      > "double *storage" and allocate it accordingly.[/color]

                      If a Triplet is const, then its members are const. So the original
                      storage is const and you cannot return a non-const reference to it.
                      But in the case of "double *storage", "storage" is still const but
                      the things it points to are non-const.

                      BCC and GCC 2 give a useful error message for the above code:
                      In method `double & Triplet::operat or [](unsigned int) const':
                      warning: conversion from `const double' to `double &' discards const
                      but GCC 3's output was obfuscated:
                      In member function `double& Triplet::operat or[](unsigned int) const':
                      error: could not convert `this->Triplet::stora ge[i]' to `double&'

                      One solution is to make two operator[] functions, one being const and
                      returning const ref, and the other non-const and returning non-const ref.
                      This is a bit inelegant (especially if you want to support volatile
                      triplets too), I don't know if there is a better solution.

                      Comment

                      Working...