Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change pair to struct with named members #1

Open
LB-- opened this issue Oct 28, 2016 · 4 comments
Open

Change pair to struct with named members #1

LB-- opened this issue Oct 28, 2016 · 4 comments
Assignees

Comments

@LB--
Copy link
Owner

LB-- commented Oct 28, 2016

https://www.kdab.com/tuple-pair-cpp-apis/

@LB-- LB-- self-assigned this Oct 28, 2016
@mosra
Copy link

mosra commented Oct 28, 2016

Interesting discussion point.

In my APIs I prefer pair/tuple over struct because std::tie() can be used on it and then I can name, reorder or retype the members as I like. That makes of course sense only if it is clear which member is which (e.g. it's either intuitively clear or the types are different that there is no possibility in mixing them up). Another point is that when using std::tie() the return type is basically anonymous set of other types, which makes the API surface area smaller -- no need to document yet another trivial struct.

On the other hand, std::*map::insert()'s return type (or its behavior in general) is just bad. I am angry every time I come across it.

@LB--
Copy link
Owner Author

LB-- commented Oct 28, 2016

I agree about std::tie, and I've been wondering if there is a way to keep it working for custom structures using some boilerplate or something.

@mosra
Copy link

mosra commented Oct 28, 2016

Structured bindings in C++17 should solve that.

Otherwise, I think that maybe just a conversion operator could do it?

#include <tuple>

struct MyReturnType {
    void* a; int b; double c;

    operator std::tuple<void*&, int&, double&>() { return std::tie(a, b, c); }
};

std::tuple<void*, int, double> foo1() { return std::make_tuple(nullptr, 3, 1.27); }
MyReturnType foo2() { return {nullptr, 3, 1.27}; }

int main() {
    void* first;
    int second;
    double third;
    std::tie(first, second, third) = foo1();
    std::tie(first, second, third) = foo2();
}

I tried to make the conversion operator const and return just a standard tuple, but neither Clang nor GCC wanted to eat it.

@mosra
Copy link

mosra commented Oct 28, 2016

Funny that in this case foo2() combines the advantages of both, usable with std::tie() and without the annoying std::make_tuple call needed pre-C++17 because the tuple constructor is explicit.

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

No branches or pull requests

2 participants