Closed DanRStevens closed 5 years ago
Thanks for working on this. I've never used friend and I'm not really familiar with constexpr so this effort seems a bit out of my knowledge right now. If we end up just sticking with the const std::array in the end I would be okay, although the easier syntax would be nice.
An intermediate option might be for a using statement to hide the const std::array<char, 4> part. It doesn't solve the awkward initialization char list though.
Hmm, you might be right in that I could probably use a using
alias instead of a new class. Might want to shorten the name of the method that creates the std::array
in that case though, or provide an alias to it. Perhaps something like:
using makeTag = stripNullTerminator<4>;
static const auto TagVol_ = makeTag("VOL ");
The constexpr
is like const
, except that it can be computed at compile time rather than run time. In some cases it's an error to pass a non-compile-time value. In other cases, such as on a method definition, you can get compile time constexpr
behaviour for literal values, while otherwise it falls back to const
and a run time call for non-literal values. Very handy, and lets you do both compile time and run time computations with the same code.
As for friend, all you need to know is that friends are allowed to touch your private members :wink:
I'd like a better way to initialize and store tags used in file processing, such as:
In particular, it would be nice if we could use a string literal
"VOL "
, while also maintaining a strictly controlled struct size of 4 bytes.I went back to the StackOverflow question about initializing a
std::array<char, N>
with a string literal, and omitting the trailing null, and did some reading and experimentation. What was posted works, and by adding a fewconstexpr
, I was able to get g++ to do compile time initialization ofstatic const
variables. I think this would be really good for what we are doing here.I'm about to take a break, so for now I'll post my experiment:
I'm uncertain if I really want to wrap
std::array
in a new type. I'm uncertain if I want the template helper methods global or wrapped in a new type or namespace. I'm uncertain if the recursive templates and parameter packs are really necessary, or if there might be a more clear way to implement this. I believe the variables could be declared asconst
rather thanconstexpr
, though usingconstexpr
may provide additional checks, and may change output.I may also want to re-read and perhaps summarize some of the relevant articles. In particular: Parameter pack
std::integer_sequence
andstd::index_sequence
constexpr
LiteralType RIP index_sequence, 2014-2017