#include <iostream>
using namespace std;
template<size_t Index, size_t N>
constexpr bool checkArrayBounds()
{
return Index < N || (throw "Index out of bounds", false);
}
template<size_t Index, typename T, size_t N>
constexpr T array_at(T(&array)[N]) // `array` is a reference to `T[N]`
{
return checkArrayBounds<Index, N>(), *(array+Index); // Comma magic *-*
}
int main()
{
constexpr int x[] { 1, 2, 3 };
static_assert(array_at<0>(x) == 1, "Error");
static_assert(array_at<1>(x) == 2, "Error");
static_assert(array_at<2>(x) == 3, "Error");
cout << array_at<0>(x) << endl;
cout << array_at<1>(x) << endl;
cout << array_at<2>(x) << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdGVtcGxhdGU8c2l6ZV90IEluZGV4LCBzaXplX3QgTj4KY29uc3RleHByIGJvb2wgY2hlY2tBcnJheUJvdW5kcygpCnsKICAgIHJldHVybiBJbmRleCA8IE4gfHwgKHRocm93ICJJbmRleCBvdXQgb2YgYm91bmRzIiwgZmFsc2UpOwp9Cgp0ZW1wbGF0ZTxzaXplX3QgSW5kZXgsIHR5cGVuYW1lIFQsIHNpemVfdCBOPgpjb25zdGV4cHIgVCBhcnJheV9hdChUKCZhcnJheSlbTl0pIC8vIGBhcnJheWAgaXMgYSByZWZlcmVuY2UgdG8gYFRbTl1gCnsKICAgIHJldHVybiBjaGVja0FycmF5Qm91bmRzPEluZGV4LCBOPigpLCAqKGFycmF5K0luZGV4KTsgLy8gQ29tbWEgbWFnaWMgKi0qCn0KCmludCBtYWluKCkKewogICAgY29uc3RleHByIGludCB4W10geyAxLCAyLCAzIH07CgogICAgc3RhdGljX2Fzc2VydChhcnJheV9hdDwwPih4KSA9PSAxLCAiRXJyb3IiKTsKICAgIHN0YXRpY19hc3NlcnQoYXJyYXlfYXQ8MT4oeCkgPT0gMiwgIkVycm9yIik7CiAgICBzdGF0aWNfYXNzZXJ0KGFycmF5X2F0PDI+KHgpID09IDMsICJFcnJvciIpOwoKICAgIGNvdXQgPDwgYXJyYXlfYXQ8MD4oeCkgPDwgZW5kbDsKICAgIGNvdXQgPDwgYXJyYXlfYXQ8MT4oeCkgPDwgZW5kbDsKICAgIGNvdXQgPDwgYXJyYXlfYXQ8Mj4oeCkgPDwgZW5kbDsKCiAgICByZXR1cm4gMDsKfQ==