I do not think it is possible to get what you want with a single function.
Even if you are developing a function constexpr, I don’t think you can detect when the runtime is being executed, and when you are compiling the time and acting differently.
But you can develop different functions.
, get<>(), , , , static_assert() at(std::size_t), , .
En passant:
1) , , STL, at() operator[]()
2), , , >= 0.
#include <iostream>
#include <stdexcept>
template <class T, std::size_t Dim>
class Foo
{
private:
T _data[Dim];
public:
T const & operator[] (std::size_t idx) const
{ return _data[idx]; }
template <std::size_t IDX>
T const & get () const
{
static_assert(IDX < Dim, "out of range");
return this->operator[](IDX);
}
T const & at (std::size_t idx) const
{
if ( idx >= Dim )
throw std::range_error("out of range");
return this->operator[](idx);
}
};
int main ()
{
Foo<float, 2U> foo;
foo.get<0U>();
foo.get<1U>();
for ( auto i = 0U ; i < 5U ; ++i )
foo.at(i);
return 0;
}