Function lookup problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Tyler

    Function lookup problem

    Could someone tell me why this function lookup doesn't work (gcc 4.1.2 on
    64bit linux)?

    struct test
    {
    const int& func( void ) const {
    static const int i = 0;
    return i;
    }

    private:
    int& func( void ) {
    return const_cast<int& >( static_cast<con st test&>(*this).f unc() );
    }
    };

    int main() {
    test t;
    int i = t.func(); // error: 'int& test::func()' is private


    return i;
    }

    I'm sure that there is a good reason for it not being able to see the
    public const version, but the thing is that I want to give read access to
    some boost tuples publicly an write access privately and I would prefer
    to keep the "tuple getting" code in one place. I know I could change the
    read function's name, or static cast t to <const test&>, but seems a bit
    awkward.

    What do you think a good solution is?

    Cheers,
    Brian
  • red floyd

    #2
    Re: Function lookup problem

    Brian Tyler wrote:
    Could someone tell me why this function lookup doesn't work (gcc 4.1.2 on
    64bit linux)?
    >
    struct test
    {
    const int& func( void ) const {
    static const int i = 0;
    return i;
    }
    >
    private:
    int& func( void ) {
    return const_cast<int& >( static_cast<con st test&>(*this).f unc() );
    }
    };
    >
    int main() {
    test t;
    int i = t.func(); // error: 'int& test::func()' is private
    >
    >
    return i;
    }
    >
    I'm sure that there is a good reason for it not being able to see the
    public const version, but the thing is that I want to give read access to
    some boost tuples publicly an write access privately and I would prefer
    to keep the "tuple getting" code in one place. I know I could change the
    read function's name, or static cast t to <const test&>, but seems a bit
    awkward.
    >
    What do you think a good solution is?
    >
    Because overload resolution and visibility are orthogonal. It finds the
    non-const version of func(), and complains because it's private.

    Your solution? Rename the non-const version of func().

    Comment

    • Victor Bazarov

      #3
      Re: Function lookup problem

      Brian Tyler wrote:
      Could someone tell me why this function lookup doesn't work (gcc
      4.1.2 on 64bit linux)?
      Why do you say it doesn't work? It works just fine giving you the
      right function as you wrote it. Perhaps you don't know the rules
      of the lookup, but that's not a gcc's problem, is it?
      >
      struct test
      {
      const int& func( void ) const {
      static const int i = 0;
      return i;
      }
      >
      private:
      int& func( void ) {
      return const_cast<int& >( static_cast<con st test&>(*this).f unc()
      ); }
      };
      >
      int main() {
      test t;
      int i = t.func(); // error: 'int& test::func()' is private
      It *is* private, and accessing private members from a non-friend, non-
      member *is* prohibited. Why are you surprised?
      >
      >
      return i;
      }
      >
      I'm sure that there is a good reason for it not being able to see the
      public const version,
      It sees it fine. It also sees the private non-const "version",
      and the rules of overload resolution tell the compiler to pick
      the non-const version (the private one) because 't' isn't const.
      but the thing is that I want to give read
      access to some boost tuples publicly an write access privately and I
      would prefer to keep the "tuple getting" code in one place. I know I
      could change the read function's name, or static cast t to <const
      test&>, but seems a bit awkward.
      >
      What do you think a good solution is?
      As you point out, "read" and "write" are different actions, so
      you should probably name your functions differently.

      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask


      Comment

      • Brian Tyler

        #4
        Re: Function lookup problem

        I had guessed as much, but thought that some sort of automatic conversion
        might take place... reminds me of my introduction operators and
        namespaces.

        I will rename the read function.

        Thanks for the help.

        Brian

        Comment

        • Chris Thomasson

          #5
          Re: Function lookup problem

          "Brian Tyler" <brian.tyler@gm ail.comwrote in message
          news:AaPKj.4422 8$kN5.9791@news fe1-gui.ntli.net...
          Could someone tell me why this function lookup doesn't work (gcc 4.1.2 on
          64bit linux)?
          [...]
          >
          Your calling func on a non-const test object. Try this to force it work:
          _______________ _______________ _______________ _________
          struct test
          {
          const int& func( void ) const {
          static const int i = 0;
          return i;
          }

          private:
          int& func( void ) {
          return const_cast<int& >( static_cast<con st test&>(*this).f unc() );
          }
          };

          int main() {
          test t;
          test const& tx = t;
          int i = tx.func();

          return i;
          }
          _______________ _______________ _______________ _________



          What do you think a good solution is?
          Rename your private function. I personally like to add either a 'sys_' or a
          'prv_' prefix to my private function names. In other words use
          'test::sys/prv_func()' instead...

          Comment

          Working...