On Friday, 10 January 2020 14:18:47 UTC+2,
bol...@nowhere.org wrote:
> On Thu, 9 Jan 2020 12:42:23 -0800 (PST)
> Öö Tiib <
oot...@hot.ee> wrote:
> >On Thursday, 9 January 2020 11:38:04 UTC+2,
bol...@nowhere.org wrote:
> >> Morning
> >>
> >> Is there a way to overload get so that when used on a user defined class it
> >> will return values defined by the user , not simple an index value? Or to
> >> put it another way, what do standard classes like tuple have to implement
> >> in order for get<>() to work with them?
> >>
> >> eg:
> >>
> >> struct myclass
> >> {
> >> blah b;
> >> int i;
> >> string s;
> >> :
> >> :
> >> };
> >>
> >>
> >> cout << get<0>(obj); <- How would I make this return string s for example?
> >
> >The std::tuple is variadic template and so its elements are
> >enumerated during its generation. For such stinky myclass it
> >is most trivial to make those manually.
>
> If quite capable of writing a class method called get() though I wouldn't use
> such a ridiculously complex method that you have, I wanted to how to use
> the standard library get() for my classes as per tuple and variant.
It wasn't clear from you OP what you are capable of (if anything).
There is only small semantic difference if something is
implemented to use syntax "a.foo(b)" or "foo(a,b)". I prefer
"a.foo(b)" when the parameter "a" is a reference of existing
object.
> It can be
> done with a tuple cast overload method in the class but unfortunately when
> using get() it requires an explicit cast in the call. eg:
>
> auto val = get<0>((tuple<string,int,int>)(myobj));
>
> Which works but is ugly.
In sense of standard your C-style cast (that is effectively
reinterpret_cast and is warned about by lot of tools) is not
"ridiculously simpler". The std::tuple is not required to be of
standard layout. The std::string (I suspect one of members
of your myclass is supposed to be std::string) is not standard
layout and so neither that tuple nor your myclass is standard
layout. So reinterpret_cast there is undefined behavior.
Even if some compiler provides extensions (can you cite what
compiler and where?) it is non-portable.
The "std::get" is not a customization point for the standard
library. The overloads (for pair, tuple and array) do not allow for
user-defined overloads explicitly. So adding a declaration of
your own "get" overload into namespace "std" is undefined
behaviour.
You can add non-member "get" into same namespace as
your "myclass" is. Then ADL will make it to work for "get" like
it already works on case of std::pair, std::tuple or std::array.
But do not use reinterpret_cast. Do like I suggested or tie
like other poster suggested.