[variant] assign and access the assigned value simultaneously
hi list,
for common case:
boost::variant
On 14/03/2014 19:22, Quoth feverzsj:
for common case:
boost::variant
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.)
2014-03-14 10:22 GMT+04:00 feverzsj
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
2014-03-19 15:13 GMT+08:00 Antony Polukhin
2014-03-14 10:22 GMT+04:00 feverzsj
: <...> 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.
AMDG On 03/19/2014 12:26 AM, TONGARI J wrote:
2014-03-19 15:13 GMT+08:00 Antony Polukhin
: 2014-03-14 10:22 GMT+04:00 feverzsj
: <...> 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
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. -- View this message in context: http://boost.2283326.n4.nabble.com/variant-assign-and-access-the-assigned-va... Sent from the Boost - Dev mailing list archive at Nabble.com.
participants (5)
-
Antony Polukhin
-
feverzsj
-
Gavin Lambert
-
Steven Watanabe
-
TONGARI J