samedi 28 février 2015

Ambiguous base class conversion with a compressed pair

So I tried creating a compressed pair using the empty base optimization. I would like it such that if class a and b are empty then compressed_pair<a, b> is empty as well. So I defined my compressed pair something like this:



template <class First, class Second>
struct compressed_pair : First, Second
{
compressed_pair() {}
compressed_pair(const First& x, const Second & y)
: First(x), Second(y)
{}
First& first() { return *this; }
Second& second() { return *this; }
};


However, if one of the types inherit from the other it becomes ambiguous. For example, when I compile this program:



struct a
{};

struct b : a
{};

int main()
{
compressed_pair<a, b> p;
auto x = p.first();
}


I get this error from clang:



compressed_pair.cpp:8:30: error: ambiguous conversion from derived class 'compressed_pair<a, b>' to base class 'a':
struct compressed_pair<struct a, struct b> -> struct a
struct compressed_pair<struct a, struct b> -> struct b -> struct a
First& first() { return *this; }
^~~~~
compressed_pair.cpp:21:16: note: in instantiation of member function 'compressed_pair<a, b>::first' requested here
auto x = p.first();
^


So how can I avoid the ambiguous conversion and still have compressed_pair<a, b> be empty?


Aucun commentaire:

Enregistrer un commentaire