by jcelerier · 5y ago · 0 comments
struct JSONValue {
std::optional<std::string> string;
std::optional<double> number;
std::optional<bool> boolean;
std::optional<std::vector<JSONValue>> array;
std::optional<std::map<std::string, JSONValue>> object;
JSONValueType type;
};
really ought to be a variant, that would simplify things a lot. It'd just be struct JSONValue;
using variant_type =
std::variant<
std::string
, double
, bool
, std::vector<JSONValue>
, std::map<std::string, JSONValue>
>;
struct JSONValue : variant_type {
using variant::variant;
};
then instead of switches you'd do: std::string_view print_type(const variant_type& jtt) {
using namespace std::literals;
struct {
auto operator()(const std::string&) const noexcept { return "String"sv; }
auto operator()(double) const noexcept { return "Number"sv; }
auto operator()(bool) const noexcept { return "Bool"sv; }
auto operator()(const std::vector<JSONValue>&) const noexcept { return "Array"sv; }
auto operator()(const std::map<std::string, JSONValue>&) const noexcept { return "Dict"sv; }
auto operator()(std::monostate) const noexcept { return "Null"sv; }
} vis;
return std::visit(vis, jtt);
}
which is much safer than switch/cases std::string_view print_type(const variant_type& jtt) {
using namespace std::literals;
return inpect (jtt) {
<double> __ => "Number"sv;
<bool> __ => "Bool"sv;
...
};
}
In any case, `std::variant` should have been a language feature all along, instead of a library one; cf. Rust: enum Value {
Number(f64),
Bool(bool),
...
}
fn print_type(v: &Value) -> &str {
match v {
Value::Number(_) => "Number",
Value::Bool(_) => "Bool",
...
}
} std::string format_parse_error(std::string base, JSONToken token) {
std::ostringstream s;
s << "Unexpected token '" << token.value << "', type '"
<< JSONTokenType_to_string(token.type) << "', index ";
s << std::endl << base;
return format_error(s.str(), *token.full_source, token.location);
}
becomes std::string format_parse_error(std::string_view base, JSONToken token) {
return format_error(
std::format(
"Unexpected token '{}', type '{}', index '{}'\n{}"
, token.value
, JSONTokenType_to_string(token.type)
, index
, base)
, *token.full_source
, token.location
);
}, you'd put your commas where normal human beings put them
. Instead of in a silly place like that
. Prefering structure over normal communication is not
, typically
, seen as a good thing
.
Programming is not prose, so we can temper our expectations. *
Even in prose, some symbols belong on the left, and some belong on the right. *
The example given is no less readable than the alternative. *
It's more important to be consistent in style than correct to one person's opinion e.g. some people still batch edit source via macros and appreciate when curly braces are not in unexpected places.
it just removes a lot of hassle when you are used to it, I'd never go back to trailing commas
I didn't want to have to spring a newer compiler version than came with the distro.
Unless it's just a flag I did not turn on.
Still, thanks for sharing so that other people can see for themselves.
One of the biggest turnoffs about std::variant to me (which isn't relevant here) is that you can't name the types so the hacks for having multiple of the same type in std::variant look even hairier.
- your JSONStruct takes 160 bytes (on 64-bit systems with libstdc++), the variant one takes 56, almost three times less.
- it needs std::optional<T> for each member to avoid construction, while std::variant does that by default. But the compiler still have to initialize all these optionals to their default state.
- you need on every access two branches:
* one for the type switch
* one for the optional
the variant only has one level of indirection.
Actually I was most interested in just how simple and readable I could make it. But I didn't say that in the post anywhere. Will add.
It's just not possible to assume that at all on the internet. Every year I have students who learn from blog posts like this for instance.
If you did want squeeze out the extra performance you could also do something where you store `std::variant<>` inside a struct and provide an API like this:
if (auto int_value = value.i()) {
// *int_value == your int
}
and expose a `std::variant<> &variant();` for use cases where you want the more complex/faster stuff. struct string_view {
char *data;
size_t length;
}
It is a pointer to a subset of an existing string. It has all of the existing features of `std::string` but passing it around is zero copy. As long as the original memory allocation exists your `string_view`s are still valid. Also, it has an implicit constructor from `const std::string` and `const char #` allowing you you define a single function like `Thing ParseThing(std::string_view line)` and accept `char #` and `std::string` as an input.More examples are here: https://abseil.io/tips/1
# -> *. HN formats bad
Reference this paper for further details:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n451...
It's a bit like naming a global variable _Weeeeeeeeee. It's technically UB because the standard says so, but in practice the compiled code will be fine if your stdlib does not use that variable, the compiler isn't actively looking for elements of the spec to check and enforce, it's just using UB where it can as a tool to optimize
I don't see the purpose in allowing trivially detectable UB to exist. In this case and in the case of reserved identifiers, I don't see how any optimization could be made using the UB. Noticing the UB allows you to do anything, so you might as well rewrite the program to do nothing.