Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

How to annotate an owner properly? #97

Open
@brevzin

Description

@brevzin

Here's a simple example:

#include <vector>
#include <string>

void use(char);
auto f() -> std::vector<std::string>;

int main() {
    for (char c : f()[1]) {
        use(c);
    }
}

This, correctly and awesomely, issues a warning on the for loop because of the object being destroyed. Cool.

Now let's say I have my own custom vector of strings, which I'm calling VS because I'm not creative:

#include <vector>
#include <string>

void use(char);

struct [[gsl::Owner]] VS {
    auto operator[](size_t i) -> std::string&;
    auto begin() -> std::vector<std::string>::iterator;
    auto end() -> std::vector<std::string>::iterator;

    std::vector<std::string> v;
};

auto f() -> VS;

int main() {
    for (char c : f()[1]) {
        use(c);
    }
}

This does not warn, even though it's basically the same example and I tried to annotate VS as being an owner. Is there some way to still get a warning for such code?

Obligatory compiler explorer link.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions