How can someone make this code compile:
#include <iostream>
using namespace std;
enum E { A, B};
template< E x>
class C {
public:
#if( x == A)
static void foo() {
cout << "A";
}
#elif( x == B)
static void goo() {
cout << "B";
}
#endif
};
int main() {
C< A>::foo();
C< B>::goo();
return 0;
}
error: ‘goo’ is not a member of ‘C<(E)1u>’
I have two big classes which differs only a few lines, so I wanted to make enum template. The problem is that this lines have using
keyword in them, so I don't know what to put there.
Is there some correct way to do it?
PS> I'm have to use C++03.
EDIT: A little clarification. I know about template<>
construction, but I don't wanted to use it because this way I got a lot of code duplication. Maybe I can somehow make partial "template instanciation" (if this is correct term for template<>
)?
Suppose I have two classes (and I will not have more):
class A {
public:
//…a lot of code…
//few lines that differs in A and B
}
class B {
//…the same mass of code…
//few lines that differs in A and B
}
So I decided to make template on enum
:
enum E { A, B}
template< E>
class C{
//…common code…
}
Now I don't know what to do with this few lines that differs in A and B. I know that common way is to make template instanciation, but then I'll get exactly what I had with classes A
and B
.
From the OOP point I should use common Base
for A
and B
. But the problem is that A
and B
is already the same. They differs only with line:
using CanStoreKeyValue< QString, Request>::set;
using CanStoreKeyValue< QString, Response>::set;
where Response
and Request
are typedefs. Moreover, in my code A
and B
are childs of the same templated abstract class. And of course they inherit it with different template param. This somehow brokes using of template enum — compiler just can't see that some virtual methods a no longer pure. So… that's why I'm asking what I'm asking. I thought that preprocessor could interact with template engine with #if
-directives (on the end, they both are compile time processes).