Hi,
I've got a weird little problem. I've got a class that requires a pointer to a memory buffer, along with its size, at construction.
Code:
template<typename E>
class buffer
{
E *m_ptr;
std::size_t m_size;
public:
buffer(E *ptr, std::size_t size) : m_ptr(ptr), m_size(size) {}
For convenience and a bit of safety, I also offer this constructor:
Code:
template<int N>
buffer(E (&ar)[N]) : m_ptr(ar), m_size(N) {}
This way, you can construct it like this and it will infer the correct size:
Code:
char ar[5] = { 1, 2, 3, 4, 5 };
buffer<char> b(ar);
This works very nicely.
Now, I also want to provide a constructor that takes an array reference AND a size, and uses the supplied size only if it doesn't exceed the array size:
Code:
template<int N>
buffer(E (&ar)[N], std::size_t size) : m_ptr(ar), m_size(size < N ? size : N) {}
}
However, when I want to prove this works:
Code:
char ar[5] = { 1, 2, 3, 4, 5 };
buffer<char> b(ar, 7);
... it actually chooses the pointer constructor.
Thinking about it, it makes sense: the non-template is generally ordered before templates in overload resolution. However, I don't want it there.
So, does anyone have any idea how I could get the array+size constructor to be used in this situation without destroying the other use cases?