First of all, the code is restricted to C++11, so I cannot make use of if constexpr
Following is my sample code snippet:
class A{
 public:
    int a;
    int b;
}
class B{
 public:
    int key;
    int val;
}
class C{
 public:
    int n1;
    int n2;
}
class D{
 public:
    int n1;
    int n2;
}
class E{
 public:
    int n1;
    int n2;
}
template<typename T>
void func1(T data) {
   if (T == type(A)) {             // Just pseudo template-check code
    std::cout<<data.a<<data.b;              //<------1
   } else if (T == type (B)) {    // Just pseudo template-check code
    std::cout<<data.key<<data.val;          //<------2
   } else {
    std::cout<<data.n1<<data.n2;            //<------3
}
int main() {
A a;
B b;
C c;
D d;
E e;
func1(a);
func1(b);
func1(c);
func1(d);
func1(e);
return 0;
}
Currently, I get a compile-time error at,
1: B,D,E,F has no member a & b
 & 
2: A,D,E,F has no member key & val
 &
3. A, B has no member n1 & n2
I tried using is_same() & also this, but I get same compile time error every time.
- I cannot make use of C++14/C++17
 - How could I make use of specialized template functions?
 
Edited the code to highlight the need of a template.
Aucun commentaire:
Enregistrer un commentaire