Home Curiously mutually recurring class definitions
Reply: 0

Curiously mutually recurring class definitions

D. Dmitriy
1#
D. Dmitriy Published in 2018-01-12 13:57:15Z

I want type declarations in two classes to mutually depend on each other. Here is a first example that compiles both with clang and gcc:

template <class Sum>
struct A
{
    using X = char;                // (1)
    using Z = typename Sum::B::Y;  // (2)
};

template <class Sum>
struct B
{
    using Y = typename Sum::A::X;
};

struct AplusB
{
    using A = ::A<AplusB>;
    using B = ::B<AplusB>;
};

AplusB::A::Z z;

int main() {}

There is an interesting moment, however. If you swap lines (1) and (2), then it will fail to compile with an error:

error: no type named 'X' in 'A'

That makes me question whether the original code is actually valid in the sense of the C++ standard, or it just so happens to compile?

Here is a second example, which also exploits order of template instantiation:

template <class Sum>
struct A
{
    using X = char;
    using P = typename Sum::B::Q;
};

template <class Sum>
struct B
{
    using Y = typename Sum::A::X;
    using Q = int;
};

struct AplusB
{
    using A = ::A<AplusB>;
    using B = ::B<AplusB>;
};

AplusB::A::X z; // (1)
AplusB::B::Q t; // (2)

int main() {}

Here if you swap (1) and (2) it will fail to compile with error:

error: no type named 'Q' in 'B'

So the question is: Is it actually permitted by standard for class definitions to depend on each other like that?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.343545 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO