Reputation: 14869
I am using C++17 with Visual C++ 2017 and I am creating a std::tuple
of references using std::forward_as_tuple
.
Since C++14 it is possible to access an element of a tuple by using a class type instead of an index.
When I try the code below I have compilation error
error C2338: duplicate type T in get<T>(tuple)
Do you know how to access an element in a tuple created in such way?
Below the sample code
struct CA {
int data_ = 0;
};
struct CB {
int data_ = 0;
};
int main()
{
CA a;
CA b;
auto joined_objects = std::forward_as_tuple(a, b);
std::cout << std::get<0>(joined_objects).data_ << std::endl; // works
std::cout << std::get<CA &>(joined_objects).data_ << std::endl; // fails
}
Upvotes: 1
Views: 153
Reputation: 16070
It's because the compiler doesn't know which element you really want.
There is ambiguity because a
and b
have same type. get
helper for types cannot work if tuple contains same type twice. It's pretty understandable.
Did you perhaps mean to write CB b
? This works.
Upvotes: 5