I'm trying to replace a global buffer size to one that can be modifiable for tests. The current code is something like:
static const uint32_t BUFFER_SIZE = 1 << 8;
class BufferWrapper {
.
.
.
char buffer_[BUFFER_SIZE];
};
This causes a problem when I'm trying to change the BUFFER_SIZE for tests. Thus, I was hoping to make the BUFFER_SIZE a template constant, and having a default value for said constant so that I only need to specify it during tests, something like:
static const uint32_t BUFFER_SIZE = 1 << 8;
template <uint_32 SIZE = BUFFER_SIZE>
class BufferWrapper {
.
.
.
char buffer_[SIZE];
};
That way, previous declarations can still compile like:
BufferWrapper buf
But during a test, I could write something like this to test a buffer of 1KB:
BufferWrapper<1024> test_buf;
My question is, is it possible to provide a default for a template value, and how would I do it? The error I get with the above when I declare something like BufferWrapper buf; is:
error: use of class template 'BufferWrapper' requires template arguments; argument deduction not allowed in function prototype
As this answer states, the code is fine since C++17. However, if you do not have access to that, you can still get around the need to change all existing code to use BufferWrapper<> instead of BufferWrapper.
Do this by renaming BufferWrapper to something else (e.g. BufferWrapperTemplate) and providing a type alias to the default-sized version:
template <uint_32 SIZE = BUFFER_SIZE>
class BufferWrapperTemplate {
.
.
.
char buffer_[SIZE];
};
typedef BufferWrapperTemplate<> BufferWrapper;
This way, all existing code can keep using BufferWrapper, and tests can use BufferWrapperTemplate<42> where needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With