Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Some cool C++14

58 views
Skip to first unread message

Mr Flibble

unread,
Jul 10, 2018, 5:43:17 PM7/10/18
to
Whilst coding my C++ JSON parser, "NoFussJSON", I used generic lambdas for
the first time. How cool/elegant are they? Look here:

case element::Number:
{
json_string newNumber{ currentElement.start, currentElement.start ==
nextOutputCh ? nextInputCh : nextOutputCh };
if (currentState == json_detail::state::NumberInt)
{
std::visit([this, &currentElement](auto&& arg)
{
buy_value(currentElement, arg);
}, string_to_number(newNumber.as_view()));
}
else
buy_value(currentElement, neolib::string_to_double(newNumber.as_view()));
}
break;

They make using variants a breeze. The variant in question here is the
result of string_to_number function which can be int32_t, uint32_t,
int64_t, uint64_t or double depending on the parsed number string.

/Flibble

--
"Suppose it’s all true, and you walk up to the pearly gates, and are
confronted by God," Bryne asked on his show The Meaning of Life. "What
will Stephen Fry say to him, her, or it?"
"I’d say, bone cancer in children? What’s that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery
that is not our fault. It’s not right, it’s utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a
world that is so full of injustice and pain. That’s what I would say."

Daniel

unread,
Jul 11, 2018, 11:53:22 PM7/11/18
to
On Tuesday, July 10, 2018 at 5:43:17 PM UTC-4, Mr Flibble wrote:

> std::visit([this, &currentElement](auto&& arg)
> {
> buy_value(currentElement, arg);
> },

Shouldn't that be

std::visit([this, &currentElement](auto&& arg)
{
buy_value(currentElement, std::forward<decltype(arg)>(arg));
},

I think 'buy_value' should be 'by_value', no?

Daniel

Mr Flibble

unread,
Jul 12, 2018, 1:50:00 PM7/12/18
to
Yes I should be using perfect forwarding thanks however the spelling is
correct: a node is being "bought" from the value allocator.
0 new messages