Lifetime of a temporary passed in operator[](const std::string& k)

Discussion in 'C++' started by Daniel, Jun 11, 2013.

  1. Daniel

    Daniel Guest

    Is this code safe?

    class A
    {
    public:
    class Proxy
    {
    public:
    friend class A;

    operator A&()
    {
    return val_.get(k_);
    }

    operator const A&() const
    {
    return val_.get(k_);
    }
    private:
    Proxy& operator = (const Proxy& other) {/* do nothing */}
    Proxy(const Proxy& proxy)
    : val_(proxy.val_), k_(proxy.k_)
    {
    }
    Proxy(A& val, const std::string& k)
    : val_(val), k_(k)
    {
    }

    A& val_;
    const std::string& k_;
    };

    A& get(const std::string& k)
    {
    // In real code, looks up another A instance using k
    return *this;
    }

    const A& get(const std::string& k) const
    {
    // In real code, looks up another A instance using k
    return *this;
    }

    Proxy operator[](const std::string& k)
    {
    return Proxy(*this,k);
    }
    };

    A a;
    A b = a["key"];

    Do I have to worry about the lifetime of the temporary k_ held in Proxy?

    Thanks!
    Daniel
    Daniel, Jun 11, 2013
    #1
    1. Advertising

  2. Re: Lifetime of a temporary passed in operator[](const std::string&k)

    On 6/10/2013 11:13 PM, Daniel wrote:
    > Is this code safe?
    >
    > class A
    > {
    > public:
    > class Proxy
    > {
    > public:
    > friend class A;
    >
    > operator A&()
    > {
    > return val_.get(k_);
    > }
    >
    > operator const A&() const
    > {
    > return val_.get(k_);
    > }
    > private:
    > Proxy& operator = (const Proxy& other) {/* do nothing */}
    > Proxy(const Proxy& proxy)
    > : val_(proxy.val_), k_(proxy.k_)
    > {
    > }
    > Proxy(A& val, const std::string& k)
    > : val_(val), k_(k)
    > {
    > }
    >
    > A& val_;
    > const std::string& k_;
    > };
    >
    > A& get(const std::string& k)
    > {
    > // In real code, looks up another A instance using k
    > return *this;
    > }
    >
    > const A& get(const std::string& k) const
    > {
    > // In real code, looks up another A instance using k
    > return *this;
    > }
    >
    > Proxy operator[](const std::string& k)
    > {
    > return Proxy(*this,k);
    > }
    > };
    >
    > A a;
    > A b = a["key"];
    >
    > Do I have to worry about the lifetime of the temporary k_ held in Proxy?


    Unless somehow (and you don't show all the use of A::proxy an object A
    can have) the object 'b' retains the reference to the string, there is
    no need to worry. The temporary string created to be passed as the
    argument to the operator[] has the lifetime until the end of the object
    'b' initialization.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 11, 2013
    #2
    1. Advertising

  3. Daniel

    Daniel Guest

    On Tuesday, June 11, 2013 9:05:04 AM UTC-4, Victor Bazarov wrote:
    > On 6/10/2013 11:13 PM, Daniel wrote:
    >
    > > Do I have to worry about the lifetime of the temporary k_ held in Proxy?

    >
    > Unless somehow (and you don't show all the use of A::proxy an object A
    >
    > can have) the object 'b' retains the reference to the string, there is
    >
    > no need to worry. The temporary string created to be passed as the
    >
    > argument to the operator[] has the lifetime until the end of the object
    >
    > 'b' initialization.
    >
    >

    Thanks! Appreciate the response.

    Best regards,
    Daniel
    Daniel, Jun 11, 2013
    #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. pt
    Replies:
    8
    Views:
    773
  2. Geoffrey S. Knauth
    Replies:
    6
    Views:
    997
    Earl Purple
    Jan 18, 2006
  3. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  4. Replies:
    5
    Views:
    389
    James Kanze
    Jun 13, 2008
  5. Juha Nieminen
    Replies:
    2
    Views:
    272
Loading...

Share This Page