A pointer to a typedef with an incomplete type

Is there a way to declare a pointer to an incomplete type to be set using typedef in the implementation?

Here is an example of what I want:

#ifndef TEST_H #define TEST_H namespace std { class string; // THIS WON'T WORK! } struct Test { std::string *value; }; #endif 

string is a typedef for basic_string, so the code in the example will not work. I can declare an incomplete type std :: basic_string, but it looks like a workaround.

I know that the compiler will not generate characters for typedefs, and it may happen that the same name can be used in typedef for different types in different files. But since a pointer is a pointer (at least for the compiler), it should be able to do something like this.

EDIT . This is just a minimalist working example. In my real problem, I have a Facade that uses a class from a library that only Facade should know (no, it's not std :: string, and the library is not stl). I donโ€™t care about circular inclusion, but since there are a lot of files in my project that include or indirectly this facade, I am worried about compilation time, so I want to include the library file only in the Facade implementation file.

+4
source share
3 answers

No, it is not.

Indeed, for now, you just need #include <string> . This is not harmful because you cannot have a circular dependency with string : standard headers do not even know that your headers exist!

A std::string* is usually incorrect.

+5
source

The std::string forward declaration might look something like this:

 namespace std { template <class T, class Traits, class Allocator> class basic_string; template <class T> class char_traits; template <class T> class allocator; typedef basic_string<char, char_traits<char>, allocator<char> > string; } 

Example.

(This does not attempt to forward the declaration that basic_string has default template arguments. Since they can only be declared once, I suspect that only authors of the library can have enough control to disable it.)


As for polling the use of std::string* , I think no one will dynamically allocate them. However, is it not proper to refer to some other string elsewhere (or NULL)?

+3
source

Material declaration in namespace std (mostly) undefined. The only legitimate way to reference an object, function, or type of a standard library is to include the appropriate header. 17.4.3.1 says so.

+1
source

All Articles