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!