Is it possible to forward a typedef declaration that is in a namespace?

I looked around, and I can’t say whether other similar questions answer or not.

// lib.h namespace lib_namespace { struct lib_struct { typedef std::vector<LibObject> struct_value; }; typedef lib_struct::struct_value lib_value; // compiler points here }; // my.h // attempt at forward declaration namespace lib_namespace { class lib_value; }; ... // my.cpp #include "lib.h" 

I get an override compiler error that is understandable, but is there a way to forward a typedef declaration?

I intend to avoid adding lib.h as a dependency outside the library that I am creating. Perhaps there is a better way to achieve this?

Edit: To clarify, I try not to add an extra include include line to all project files that will use the library that I am creating, due to the third-party library that I am using, and the above situation I'm stuck. So if I include lib.h in my.cpp but not my.h

+4
source share
2 answers

The compiler complains because lib.h and my.h are independent header files and they do not know about each other. Moreover, unlike the actual class , typedef does not support forward declarations.

Suppose you do not want #include"lib.h" , then you can create a separate header that has typedef lib_struct::struct_value lib_value; and the corresponding part. #include this new header wherever needed.

+1
source

Namespaces are not relevant to the issue. You cannot forward a typedef declaration; only actual types (classes and structures) can be declared.

+1
source

Source: https://habr.com/ru/post/1398749/


All Articles