Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: forward declaring a type def

We're trying to use http://cpp-netlib.org and failing to compile it, because in one of its headers it has:

namespace network {
    namespace utils {
        struct thread_pool;
    }
}

and in another:

namespace network {
        namespace utils {
            typedef ::network::concurrency::thread_pool thread_pool;
    }
}

From what I understand from Forward declaration of a typedef in C++ and http://www.cplusplus.com/forum/beginner/75561 in C++ this is basically illegal - you just can't forward declare: "struct A;" and then try to "typedef X A;".

So, my question is, what on earth might they be compiling this with that allows it? Is this some new feature of C++11, since they are claiming to use the latest-greatest-of-C++11?

like image 470
DeducibleSteak Avatar asked Oct 03 '22 20:10

DeducibleSteak


1 Answers

this is basically illegal

Yes, it is.

So, my question is, what on earth might they be compiling this with that allows it?

Perhaps, they never include both headers in the same translation unit. In that case most compilers won't be able to diagnose the error (which isn't required to be diagnosed); and there's a chance that you'll still end up with a working program. You're definitely in the world of undefined behaviour, though.

Is this some new feature of C++11?

No. You can do various interesting things with using to create aliases; but you still can't declare a type as both a class and a typedef.

like image 129
Mike Seymour Avatar answered Oct 07 '22 17:10

Mike Seymour