Skip to content

Conversation

@chaoran
Copy link

@chaoran chaoran commented May 12, 2019

Otherwise the code won't compile if the '&' operater is overloaded and
return something that cannot be casted to void *.

Otherwise the code won't compile if the '&' operater is overloaded and
return something that cannot be casted to void *.
@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@chaoran
Copy link
Author

chaoran commented May 12, 2019

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@gennadiycivil
Copy link
Contributor

Thank your for this contribution. Could you please elaborate and provide more information , do you have a concrete example?
The theoretical issue is clear however we have many years of usage and I am not aware of any complaints.
Do you have a concrete example?

@chaoran
Copy link
Author

chaoran commented May 13, 2019

Here is a minimal example of what I'm trying to do. Basically I want to create a synthetic pointer class that can be used as an iterator over a buffer of fixed size strings. And I created a synthetic reference class, which has the overloaded operator& to create such pointers.

#include <gtest/gtest.h>

class string_ref;

/**
 * This is a synthetic pointer to a fixed size string.
 */
class string_ptr {
  public:
    string_ptr(const char *data, size_t size) : data_(data), size_(size) {}

    string_ptr &operator++() noexcept {
        data_ += size_;
        return *this;
    }

    string_ref operator*() const noexcept;

  private:
    const char *data_;
    size_t size_;
};

/**
 * This is a synthetic reference of a fixed size string.
 */
class string_ref {
  public:
    string_ref(const char *data, size_t size) : data_(data), size_(size) {}

    string_ptr operator&() const noexcept { return {data_, size_}; }

    bool operator==(const char *s) const noexcept {
        if (size_ > 0 && data_[size_ - 1] != 0) {
            return std::string_view(data_, size_) == std::string_view(s);
        } else {
            return std::string_view(data_) == std::string_view(s);
        }
    }

  private:
    const char *data_;
    size_t size_;
};

string_ref string_ptr::operator*() const noexcept {
    return {data_, size_};
}

TEST(string_ref, compare) {
    const char *s = "alex\0davidjohn\0";
    string_ptr ptr(s, 5);
    // EXPECT_EQ(*ptr, "alex"); // I wish I can do this, but it won't compile
    EXPECT_TRUE(*ptr == "alex");
    ++ptr;
    // EXPECT_EQ(*ptr, "david");
    EXPECT_TRUE(*ptr == "david");
    ++ptr;
    // EXPECT_EQ(*ptr, "john");
    EXPECT_TRUE(*ptr == "john");
}

@gennadiycivil
Copy link
Contributor

@chaoran Thank you for this example. Could you please also add a unit test and we can proceed after that.
Thanks again
Gennadiy

@chaoran
Copy link
Author

chaoran commented May 14, 2019

Unit test added.

@gennadiycivil
Copy link
Contributor

248165482

@gennadiycivil gennadiycivil merged commit 5b4a135 into google:master May 17, 2019
gennadiycivil added a commit that referenced this pull request May 17, 2019
PiperOrigin-RevId: 248759825
@gennadiycivil
Copy link
Contributor

note that we moved the code around a bit not to create entire new file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants