Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to forward declare a typedef that is within a namespace?

I've looked around and I can't quite tell if the other similar questions answer this 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 a redefinition compiler error which is understandable, but is there a way to forward declare that typedef?

My intent is to avoid adding lib.h as a dependency outside of the library I'm making. Perhaps there's a better way to achieve that?

Edit: To clarify, I'm trying to avoid adding an additional include directories line to all the project files that would use the library I'm creating because of the third party library I'm using and the above situation is where I'm stuck. So it's OK if I include lib.h in my.cpp, but not my.h.

like image 331
Tabalt Avatar asked Nov 04 '22 04:11

Tabalt


1 Answers

You compiler complains, because lib.h and my.h are independent header files and they don't know about each other. Moreover, unlike the actual class, the typedef doesn't support the forward declaration.

Suppose, you don't want to #include"lib.h", then you can create a separate header which has typedef lib_struct::struct_value lib_value; and the relevant part related to it. #include that new header wherever needed.

like image 118
iammilind Avatar answered Nov 09 '22 13:11

iammilind