1
#include <bit>
#include <array>

struct A {
    int a[100];
};
struct B {
    short b[200];
};

void test(const A &in) {
    const auto x = std::bit_cast<short[200]>(in); // error: no matching function for call to 'bit_cast<short int [200]>(const A&)
    const auto y = std::bit_cast<B>(in); // OK
    const auto z = std::bit_cast<std::array<short, 200>>(in); // OK
}

The initialization of x is not working, I am curious if there is a syntax to std::bit_cast to an array without extra struct, std::array, memcpy or similar helper functions.

Thanks.

1
  • But if the short is smaller than an int you can't do a bit cast. Commented Oct 3, 2022 at 13:14

1 Answer 1

4

Not even std::bit_cast can return an array, so wrapping in a class (perhaps std::array) is the best you can do.

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

Comments

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.