Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

clang

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?

like image 267
Adam Bowen Avatar asked Jul 28 '13 14:07

Adam Bowen


1 Answers

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.

like image 178
Dietmar Kühl Avatar answered Oct 21 '22 03:10

Dietmar Kühl