Say we have a class that can write material for output
class Writer
{
public:
int write(const std::string& str);
int write(const char* str, int len);
};
I was fine with that, its flexible and all that, until I realized
char* buf = new char[n];
Writer w;
w.write(buf);
This is a really nasty mistake.
We can change some patterns a little.
class WriterV2
{
public:
int write(const std::string& str);
int write(const char* str, int len);
template<typename... Args>
int write(const char*, Args...)
{ static_assert(sizeof...(Args) < 0, "Incorrect arguments"); }
};
But this method has its problems
WriterV2 w;
w.write("The templating genius!");
What should I do? What is the best design?
And before anyone asks, overloading const char (&)[N]
doesn't work . It might be possible to create a wrapper for this, but it seems ... overkill?
EDIT Adding a method write(char*)
and emitting an error is not ideal. When walking buf
around functions and all of this, it can become const char*
.