[variant] assign and access the assigned value simultaneously

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[variant] assign and access the assigned value simultaneously

feverzsj
hi list,
  for common case:
 
        boost::variant<Val,...> var;
        var = val;
        Val& v = boost::get<Val>(var);

  why not supply a interface like:

        Val& v = var.assign(val);

  for simplity, and the unnecessary "get" is also elimited.

regards
Reply | Threaded
Open this post in threaded view
|

Re: [variant] assign and access the assigned value simultaneously

Gavin Lambert
On 14/03/2014 19:22, Quoth feverzsj:
>    for common case:
>
>          boost::variant<Val,...> var;
>          var = val;
>          Val& v = boost::get<Val>(var);

Why is that a common case?  I would think that's an anti-pattern,
personally.  (But then I haven't played with variants much, so perhaps
I'm missing something.)



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [variant] assign and access the assigned value simultaneously

Antony Polukhin
In reply to this post by feverzsj
2014-03-14 10:22 GMT+04:00 feverzsj <[hidden email]>:
<...>

>   why not supply a interface like:
>
>         Val& v = var.assign(val);
>
>   for simplity, and the unnecessary "get" is also elimited.
>

Such interface is not common for C++. Most programmers are used to the
following:

class_name& class_name::operator=(const class_name&);
or
class_name& class_name::operator=(const class_internal&);

While you propose to implement `class_internal& class_name::operator=(const
class_internal&);`
This may be surprising for some users:

boost::variant<Val,...> var;
boost::variant<Val,...>& var2 = var = val;

Code from the example above will have different behavior on different
compilers (GCC won't compile it, MSVC will compile it but &var2 != &var1).
This does not look good.

--
Best regards,
Antony Polukhin

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [variant] assign and access the assigned value simultaneously

TONGARI J
2014-03-19 15:13 GMT+08:00 Antony Polukhin <[hidden email]>:

> 2014-03-14 10:22 GMT+04:00 feverzsj <[hidden email]>:
> <...>
>
> >   why not supply a interface like:
> >
> >         Val& v = var.assign(val);
> >
> >   for simplity, and the unnecessary "get" is also elimited.
> >
>
> Such interface is not common for C++. Most programmers are used to the
> following:
>
> class_name& class_name::operator=(const class_name&);
> or
> class_name& class_name::operator=(const class_internal&);
>
> While you propose to implement `class_internal& class_name::operator=(const
> class_internal&);`
>

I don't think he's proposing that for operator=, but a not-yet-exist assign
method.
Seems reasonable to me, sometimes we do want to do some immediate
operations after the assignment.

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [variant] assign and access the assigned value simultaneously

Steven Watanabe-4
AMDG

On 03/19/2014 12:26 AM, TONGARI J wrote:

> 2014-03-19 15:13 GMT+08:00 Antony Polukhin <[hidden email]>:
>
>> 2014-03-14 10:22 GMT+04:00 feverzsj <[hidden email]>:
>> <...>
>>
>>>   why not supply a interface like:
>>>
>>>         Val& v = var.assign(val);
>>>
>>>   for simplity, and the unnecessary "get" is also elimited.
>>>
>>
>> <snip>
>>
>
> <snip>
> Seems reasonable to me, sometimes we do want to do some immediate
> operations after the assignment.
>

I don't think it's worth cluttering the interface.
It's one more function that has to be documented and
tested. I don't see this as being a common use and
there's nothing stopping anyone who wants such a
function from implementing it himself as a non-member.

In Christ,
Steven Watanabe


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [variant] assign and access the assigned value simultaneously

feverzsj
In reply to this post by feverzsj
hi list,
   Let me explain this in more detail.
   Say, if one want to renew an object of some type which may be hold by a variant, and not care about the object's old data, he may do somthing like:
       
       // assure variant hold object of type T
       if(var.which() != T's index)
             var = T();                           // 2 visitation + copy
       T& t = boost::get<T>(var);     // 1 visitation
       // process t

   If we have T& t = var.assign(T()), 1 visitation can be eliminated.
   If we have T& t = var.construct<T>(...), only 1 visitation and optionally 1 in place construction are needed.

regars.