Skip to content

forward declaring classes used in template functions that do not allow incomplete types #175

@vsapsai

Description

@vsapsai

Originally reported on Google Code with ID 175

clang/llvm/iwyu trunk (3.7)

iwyu forward declares classes used in template functions that do not allow incomplete
types (e.g., std::pair<_T1,_T2>).  This is similar to the problem of forward declaring
classes used in catch().

If I have a class defined in a namespace in 

Example below of a header file:
#include <string>
#include "variant.hpp" //declares ESM::Variant
namespace ESM
{
    class ESMReader;
    class ESMWriter;
    struct Locals
    {
        std::vector<std::pair<std::string, Variant> > mVariables;
        void load (ESMReader &esm);
        void save (ESMWriter &esm) const;
    };
}

iwyu says:
components/esm/locals.hpp should add these lines:
#include <utility>                      // for pair
namespace ESM { class Variant; }
//end of file

components/esm/locals.hpp should remove these lines:
- #include "variant.hpp"  // lines 7-7

The full include-list for components/esm/locals.hpp:
#include <string>                       // for string
#include <utility>                      // for pair
#include <vector>                       // for vector
namespace ESM { class ESMReader; }  // lines 11-11
namespace ESM { class ESMWriter; }  // lines 12-12
namespace ESM { class Variant; }
end file

However, replacing #include "variant.hpp" with namespace ESM { class Variant; } causes
the error:
bits/stl_pair.h:102:11: error: field has incomplete type 'ESM::Variant'

since std::pair can't take incomplete types.

thank you! (btw, I'm not a c++ guy, CS, or would even call myself a programmer, so
please excuse any mistakes in my reports)

Reported by showard314 on 2015-02-07 17:41:53

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions