cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[SUGGESTION] Support "dot index" access for tuples as alternative for `std::get`

Open bluetarpmedia opened this issue 11 months ago • 5 comments

Cpp2 could support tuple indexing expressions like Rust and Swift as syntactic sugar for std::get.

main: () -> int = {
    bag: std::tuple = (1, "test", 3.14);
    std::cout << bag.1 << "\n";         // Instead of std::get<1>(bag)
    return bag.0;                       // Instead of std::get<0>(bag)
}

Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code? No

Will your feature suggestion automate or eliminate X% of current C++ guidance literature? No

bluetarpmedia avatar Mar 18 '24 01:03 bluetarpmedia

Seems like something you could implement with Cpp1 reflection (I think it's in the paper).

JohelEGP avatar Mar 18 '24 01:03 JohelEGP

Looks like operator[] might be an option for accessing tuple members because in terms of usage, tuple is a collection that can be accessed with indexes. Also for example in Python the interface of tuple is similar to list with accessing with [index].

fro0m avatar Mar 22 '24 13:03 fro0m

The index needs to be known at compile time though, as the return type depends on it.

jcanizales avatar Mar 25 '24 17:03 jcanizales

The index needs to be known at compile time though, as the return type depends on it.

Yes, the tup.1 syntax proposed above would be lowered to std::get<1>(tup).

bluetarpmedia avatar Mar 25 '24 22:03 bluetarpmedia

I know I was replying to the suggestion to use operator[]. Although maybe such a member function can indeed be written with Cpp2's existing tools?

jcanizales avatar Mar 25 '24 23:03 jcanizales