Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to a typedef with incomplete type

Tags:

c++

Is there any way to declare a pointer to an incomplete type that will be set by a typedef in the implementation?

Here is exemple 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 to basic_string, so the code in the exemple won't work. I could declare an incomplete type of std::basic_string, but thats looks like a workaround.

I know that the compiler won't generate symbols for typedefs, and it could happen that the same name could be used in typedefs for different types in different files. But since a pointer is a pointer (at least to the compiler), it should be possible to do something like that.

EDIT: This is just a minimalist working exemple. In my real problem, I have a Facade which uses a class from a library that only the Facade should need to know (no, it's not std::string, and the library is not stl). I'm not really worried with circular inclusion, but since a lot of files in my project include this Facade (directly or indirectly), I am worried with compile time, so I want to include the library file only in the Facade's implementation file.

like image 243
fbafelipe Avatar asked Jan 20 '23 04:01

fbafelipe


1 Answers

No, it's not.

Really, at this point, you're just going to have to #include <string>. It's not harmful because you can't have a circular dependency with string: standard headers don't even know that your headers exist!

A std::string* is usually wrong, anyway.

like image 125
Lightness Races in Orbit Avatar answered Jan 31 '23 04:01

Lightness Races in Orbit