Reputation: 706
Given the scenerio,
template <bool T>
struct A{
struct B{
int x;
double y;
}
struct C{
char c;
std::string k;
}
using type = std::conditional<T, B, C>::type;
type b;
}
I would like to access to data members such as:
int main(){
A<true> object_1;
A<false> object_2;
cout << object_1.x;
cout << object_2.k;
}
This would be possible if .
(dot) operator could be overloaded. However, this is not possible(at least now). Is there a workaround to make the example work?
Upvotes: 0
Views: 35
Reputation: 706
Seems that it is easier than I thought :
struct B{
int x;
double y;
}
struct C{
char c;
std::string k;
}
template <bool T>
struct A : public std::conditional <T, B, C>::type{
}
Upvotes: 0
Reputation: 6002
You cannot overload .
, that is correct. You can, however, overload ->
, if that is also an option for you?
operator->
must return a type that itself supports operator->
, e.g. a raw pointer suits this:
template <bool T> struct A
{
struct B
{
int x;
double y;
};
struct C
{
char c;
std::string k;
};
using type = std::conditional<T, B, C>::type;
type b;
type* operator->() { return &b; }
};
You can use it like so:
int main()
{
A<true> object_1;
A<false> object_2;
cout << object_1->x;
cout << object_2->k;
}
Upvotes: 1