1

In the following example, I define a basic statically-sized container class. I want to specifically use integer arithmetic to expand the size of the container. Consider the following example (that does not compile when the line auto arr2 = arr+3; is included):

template <const int val> struct int_const_t
{
    constexpr static int value = val;
};

template <typename data_t, const std::size_t ar_size> struct container_t
{
    data_t data[ar_size];

    template <const int rhs_val>
    container_t<data_t, rhs_val+ar_size>
    operator+(const int_const_t<rhs_val>& rhs)
    {
        return container_t<data_t, rhs_val+ar_size>();
    }
};

int main(int argc, char** argv)
{
    container_t<float, 4> arr;

    //I want arr2 to be of type container_t<float, 7>
    auto arr2 = arr+3; // <-- doesn't compile

    return 0;
}

It is clear to me that there is another simple solution to this, but the project I am working on specifically requires this behaviour. In short, if an integer is known at compile-time, I want to be able to capture it as a template parameter without changing the usage syntax. Is it possible to create this behaviour somehow?

I am aware that it is not possible to use function parameters as compile-time constants, so I realize that the solution (if it exists) might look a little different.

Bonus if the solution works for floating-point types!

wvn
  • 624
  • 5
  • 12
  • 2
    Seems impossible, you should only be able to `auto arr2 = arr+int_const_t<3>{};`. – 康桓瑋 Aug 04 '22 at 01:37
  • 1
    Agreed, this is un-possible. All overloads boil down to function (or class method) calls, when all is said and done. And function parameters cannot be compile-time constants. – Sam Varshavchik Aug 04 '22 at 02:10
  • @RaymondChen Not quite: the subtle difference here is whether or not I can capture the integer constant in a type rather than as an integer function parameter. – wvn Aug 04 '22 at 10:31

0 Answers0