5

Is this really the best way to declare a byte (or array of bytes)?

static constexpr byte kGuard1_[] = {
            byte{0x45}, byte{0x23}, byte{0x12}, byte{0x56}, byte{0x99}, byte{0x76}, byte{0x12}, byte{0x55},
        };

why isn't there some suffix (like b) that you can use to directly mark the number as a byte? Or is the problem just with my use of uniform initialization?

1 Answer 1

8

I can't say if this is better but it's different since you won't have to repeat byte for every element:

#include <array>
#include <utility>

template <class T, class... Args>
constexpr auto mkarr(Args&&... args) {
    return std::array{static_cast<T>(std::forward<Args>(args))...};
}

static constexpr auto kGuard1 = mkarr<std::byte>(0x45, 0x23, 0x12, 0x56,
                                                 0x99, 0x76, 0x12, 0x55);

Note that it uses a std::array<std::byte, 8> instead of a std::byte[8].


why isn't there some suffix (like b) that you can use to directly mark the number as a byte?

I can't say, but if you want you can define your own user-defined literal that could be used with C arrays.

constexpr std::byte operator""_B(unsigned long long x) {
    // note: no space between "" and _B above
    return static_cast<std::byte>(x); 
}

static constexpr std::byte kGuard1[]{0x45_B, 0x23_B, 0x12_B, 0x56_B,
                                     0x99_B, 0x76_B, 0x12_B, 0x55_B};

Or is the problem just with my use of uniform initialization?

No, it's just that there is no implicit conversion to std::byte.

Sign up to request clarification or add additional context in comments.

14 Comments

Leaning towards adding the operator""_B thing, but doesn't work well cuz - in what namespace do I add it? The right answer is std, but thats illegal. And the mkarr() thing, is also terrible. Declaring arrays of bytes is not unusual. I could add a library routine mkarray, but seems to have very few use cases but this, and overkill for this one case. Sigh, but thank you!
note in case not clear, the issue about namespaces with operator""_B, is that it only applies if you have done 'using' of that namespace.
@lewis The note I made was to make that explicit. No infringement on the standard library.
Not sure what your comment about 'the note i made'. If you are referring to the but about explicit conversions, that's why I said uniform initialization could be the problem (since then no requirement about implicit conversions). Anyhow - leaning towards your _B approach. I just wish this was in the standard library so I didn't need to do a using namespace 'some_utilities' to be able to use it.
You didn't answer it perfectly or completely (I was hoping to hear this was going to be fixed in the std library, or a reason why it didn't make sense to add, or another coding pattern that makes this work simply and naturally).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.