By the C++ standard n3376, the following code is ill-formed.
using cell = pair<void*, cell*>; // ill-formed
Is there any way to implement cell
, which is an reasonable data structure, without defining an new cell
class in C++?
By the C++ standard n3376, the following code is ill-formed.
using cell = pair<void*, cell*>; // ill-formed
Is there any way to implement cell
, which is an reasonable data structure, without defining an new cell
class in C++?
At the cost of (quite some) readability, you may use a custom pair
and some specialization to achieve something similar
struct self {};
template<typename F, typename S>
struct pair
{
F first;
S second;
// ...
};
template<typename F>
struct pair<F, self*> : pair<F, pair*>
{
};
And you write
using cell = pair<void*, self*>;
As far as I know, your only option is inheritance, i.e.
struct cell : std::pair<void*,cell*> { };
If you need cell
to be of the same type as std::pair<void*,cell*>
, you could cast the former to the latter at the point where that matters.
Another option could be to use
using cell = pair<void*, void*>;
and then cast the second element to the right type when necessary. You could write a function for that, like
cell& get_cell(cell& x) {
return *static_cast<cell*>(x.second);
}