Static_assert by indexes that are known at compile time

Is there a way to statically assert indexes known at compile time and runtime otherwise? Example:

template <class T, int Dim>
class Foo
{
    T _data[Dim];
    public:
        const T &operator[](int idx) const
        {
            static_assert(idx < Dim, "out of range"); // error C2131: expression did not evaluate to a constant
            return _data[idx];
        }
};

int main()
{
    Foo<float, 2> foo;

    foo[0];
    foo[1];
    foo[2]; // compiler error

    for (int i=0; i<5; ++i)
    {
        foo[i]; // run time assert when i > 1
    }

    return 0;
}
+4
source share
2 answers

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>();
   //foo.get<2U>(); // compiler error

   for ( auto i = 0U ; i < 5U ; ++i )
      foo.at(i); // run time exception when i > 1

   return 0;
 }
+1

. constexpr. , constexpr. , gcc , .

+3

Source: https://habr.com/ru/post/1669534/


All Articles