Search code examples
c++c++11clang

Forward declaring the mapped type of a map and C++11


Most C++ compilers I've worked with accept the following

#include <map>

struct A;

struct B
{
    typedef std::map<int,A>::iterator iterator;
    std::map<int,A> test;
};

struct A
{
};

int main()
{
    return 0;
}

however, Apple clang 4.0 compiled with

clang++ test.cpp -o test -std=c++11 -stdlib=libc++

produces a collection of errors that imply A must be a complete type before std::map can be used. Is this a defect in the libc++ implementation of map, a new requirement imposed by C++11 or a bad assumption on my part?


Solution

  • According to the requirements in 17.6.4.8 [res.on.functions] paragraph 2 it states:

    In particular, the effects are undefined in the following cases: ... if an incomplete type (3.9) is used as a template argument when instantiating a template component, unless specifically allowed for that component.

    Few components explicitly state that template arguments are allowed to be incomplete. That, is you are making an assumption which is not covered by the standard.