I want to create typesafe structures that are basically identical but have different types so that they require different function signatures.
struct A {
Time t;
void doStuff(const A&);
A getStuff();
};
struct B {
Time t;
void doStuff(const B&);
B getStuff();
};
If I sue a template for the class
template<class T>
struct X {
Time t;
void doStuff(const X&);
X getStuff();
};
how can I make functions typesafe and define function signatures differently for a struct X of type A and a struct X of type B?
Try adding some unused template parameters.
template <int>
struct X{
Time t;
void doStuff(const X&); // You missed return type
X getStuff();
}; // You missed a semicolon
// Great thanks to "aschepler"
And now you can (C++11 syntax)
using A = X<1>;
using B = X<2>;
// typedef X<1> A;
// typedef X<2> B;
The following code will fail, which is what you want:
A a; B b;
a.doStuff(b); // Fail
a = b.getStuff(); // Fail