Consider the following code where the Writer_I
acts as an interface. Other classes which fulfil the contract of writing element types in correct form can derive
here are most of the errors gone except for line 41 #include #include
enum class Elem {
HEADER,
FOOTER,
};
template class Writer_I {
public:
template decltype(auto) write(T &&...args) {
return static_cast(this)->template write(
std::forward(args)...);
}
virtual ~Writer_I() {}
};
class Streams : public Writer_I {
public:
template >
void write(int a) {
std::cout << a << std::endl;
}
template >
void write(float a) {
std::cout << "\n-------\n" << a << std::endl;
}
};
/* Restrictions being that member functions header and footer
remain in cpp files. And creators of Calculator
can specify alternative implementations. */
class Calculator {
std::_Any_tag writer;
public:
template
Calculator(Writer_I& writer) : writer(writer) {}
template void write_i(T &&...args) {
/* */ writer(std::forward(args));
}
void header() {
for (int i = 0; i < 10; i++) {
write_i(i);
}
}
void footer() {
write_i(-100.0f);
}
};
int main() {
Streams streams;
Calculator calc(streams);
calc.header();
return 0;
}