struct S
{
S(int);
S(std::string);
void foo(int);
void foo(std::string)
};
So my problem is that foo() should be invokable only with the type the ctor was. Solutions I can think of and problems with them:
- Template the whole class. However this brings all the implementation details to the header, polluting it with many library
#includes, making the API hard to read, making error messages long. bool is_intruntime check. Theassert()could be forgotten when implementing this API. The constraint is not evident from the API.- Separate classes. Violates DRY or forces all implementation to be extracted in .cpp-local functions - which is not so readable nor intuitive.
What other solutions are there? Or which of mine is the least silly?