c++ - Make custom type "tie-able" (compatible with std::tie) -



c++ - Make custom type "tie-able" (compatible with std::tie) -

consider have custom type (which can extend):

struct foo { int a; string b; };

how can create instance of object assignable std::tie, i.e. std::tuple of references?

foo foo = ...; int a; string b; std::tie(a, b) = foo;

failed attempts:

overloading assignment operator tuple<int&,string&> = foo not possible, since assignment operator 1 of binary operators have members of left hand side object.

so tried solve implementing suitable tuple-conversion operator. next versions fail:

operator tuple<int,string>() const operator tuple<const int&,const string&>() const

they result in error @ assignment, telling "operator = not overloaded tuple<int&,string&> = foo". guess because "conversion type x + deducing template parameter x operator=" don't work together, 1 of them @ once.

imperfect attempt:

hence tried implement conversion operator exact type of tie:

operator tuple<int&,string&>() const   demo operator tuple<int&,string&>()   demo

the assignment works since types (after conversion) same, won't work 3 scenarios i'd support:

if tie has variables of different convertible types bound (i.e. alter int a; long long a; on client side), fails since types have match. contradicts usual utilize of assigning tuple tuple of references allows convertible types.(1) the conversion operator needs homecoming tie has given lvalue references. won't work temporary values or const members.(2) if conversion operator not const, assignment fails const foo on right hand side. implement const version of conversion, need hack away const-ness of members of const subject. ugly , might abused, resulting in undefined behavior.

i see alternative in providing own tie function + class "tie-able" objects, makes me forcefulness duplicate functionality of std::tie don't (not find hard so, feels wrong have it).

i think @ end of day, conclusion 1 drawback of library-only tuple implementation. they're not magic we'd them be.

edit:

as turns out, there doesn't seem real solution addressing of above problems. reply explain why isn't solvable. in particular, i'd shed lite on why "failed attempts" can't perchance work.

(1): horrible hack write conversion template , convert requested fellow member types in conversion operator. it's horrible hack because don't know store these converted members. in this demo utilize static variables, not thread-reentrant.

(2): same hack in (1) can applied.

why current attempts fail

std::tie(a, b) produces std::tuple<int&, string&>. type not related std::tuple<int, string> etc.

std::tuple<t...>s have several assignment-operators:

a default assignment-operator, takes std::tuple<t...> a tuple-converting assignment-operator template type parameter pack u..., takes std::tuple<u...> a pair-converting assignment-operator template 2 type parameters u1, u2, takes std::pair<u1, u2>

for 3 versions exist copy- , move-variants; add together either const& or && types take.

the assignment-operator templates have deduce template arguments function argument type (i.e. of type of rhs of assignment-expression).

without conversion operator in foo, none of assignment-operators viable std::tie(a,b) = foo. if add together conversion operator foo, default assignment-operator becomes viable: template type deduction not take user-defined conversions account. is, cannot deduce template arguments assignment-operator templates type foo.

since 1 user-defined conversion allowed in implicit conversion sequence, type conversion operator converts must match type of default assignment operator exactly. is, must utilize exact same tuple element types result of std::tie.

to back upwards conversions of element types (e.g. assignment of foo::a long), conversion operator of foo has template:

struct foo { int a; string b; template<typename t, typename u> operator std::tuple<t, u>(); };

however, element types of std::tie references. since should not homecoming reference temporary, options conversions within operator template quite limited (heap, type punning, static, thread local, etc).

c++ c++11 tuples std

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -