Questions tagged [incomplete-type]

In the C and C++ languages, an incomplete type is "lacking sufficient information to determine the size of objects of that type." (Examples: variable-length arrays, forward declarations of `struct`s, and the `void` type.)

In the C and C++ programming language, an incomplete type is

"lacking sufficient information to determine the size of objects of that type."ISO C11 standard, section 6.2.5

Examples:

The standard mentions the following three instances of incomplete types:

  1. an array type of variable, or unknown size, such as char *argv[].
  2. a type that describes an object but lacks information to determine their sizes, such as struct foo_tag;
  3. the void type (which, unlike the other two, cannot be completed)

Resources:

310 questions
221
votes
6 answers

std::unique_ptr with an incomplete type won't compile

I'm using the pimpl-idiom with std::unique_ptr: class window { window(const rectangle& rect); private: class window_impl; // defined elsewhere std::unique_ptr impl_; // won't compile }; However, I get a compile error regarding…
user1203803
49
votes
3 answers

Can't a class have static constexpr member instances of itself?

This code is giving me incomplete type error. What is the problem? Isn't allowed for a class to have static member instances of itself? Is there a way to achieve the same result? struct Size { const unsigned int width; const unsigned int…
nyarlathotep108
  • 4,663
  • 1
  • 16
  • 46
39
votes
8 answers

"Incomplete type" in class which has a member of the same type of the class itself

I have a class that should have a private member of the same class, something like: class A { private: A member; } But it tells me that member is an incomplete type. Why? It doesn't tell me incomplete type if I use a pointer, but I'd…
Sterling
  • 3,585
  • 13
  • 44
  • 72
35
votes
6 answers

Incompatible types List of List and ArrayList of ArrayList

The below line gives me error : Incompatible Types. List> output = new ArrayList>(); What is the reason? EDIT I understand if I change my second ArrayList to List, it does not give me error. I want to know the…
Kraken
  • 20,468
  • 32
  • 90
  • 145
32
votes
3 answers

GCC: Array type has incomplete element type

I have declared a struct, and I try to pass an array of those structs (as well as a double array of doubles, and an integer) into a function. I get an "array type has incomplete element type" message from gcc when I compile it. What have I gotten…
Joshua Soileau
  • 2,715
  • 7
  • 34
  • 49
28
votes
3 answers

Delete objects of incomplete type

This one made me think: class X; void foo(X* p) { delete p; } How can we possibly delete p if we do not even know whether X has visible destructor? g++ 4.5.1 gives three warnings: warning: possible problem detected in invocation of delete…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
27
votes
3 answers

Is &*p valid C, given that p is a pointer to an incomplete type?

Is the following example a valid complete translation unit in C? struct foo; struct foo *bar(struct foo *j) { return &*j; } struct foo is an incomplete type, but I cannot find an explicit prohibition of dereferencing an incomplete type in the…
caf
  • 216,678
  • 34
  • 284
  • 434
21
votes
2 answers

Does the GotW #101 "solution" actually solve anything?

First read Herb's Sutters GotW posts concerning pimpl in C++11: GotW #100: Compilation Firewalls (Difficulty: 6/10) GotW #101: Compilation Firewalls, Part 2 (Difficulty: 8/10) I'm having some trouble understanding the solution proposed in GotW…
Ben Voigt
  • 260,885
  • 36
  • 380
  • 671
20
votes
4 answers

Equivalence of p[0] and *p for incomplete array types

Consider the following code (it came about as a result of this discussion): #include void foo(int (*p)[]) { // Argument has incomplete array type printf("%d\n", (*p)[1]); printf("%d\n", p[0][1]); // Line 5 } int…
Oliver Charlesworth
  • 252,669
  • 29
  • 530
  • 650
19
votes
1 answer

Is it legal to call delete on a null pointer of an incomplete type?

And if so, why does the following code give me the warning note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined ? struct C; int main() { C *c = nullptr; …
Dan M.
  • 3,410
  • 1
  • 20
  • 34
19
votes
1 answer

Why C++ containers don't allow incomplete types?

Why doesn't C++ allow containers of incomplete types to be instantiated? It's certainly possible to write containers that don't have this restriction -- boost::container is completely capable of doing this. As far as I can see, it doesn't seem to…
user541686
  • 189,354
  • 112
  • 476
  • 821
18
votes
3 answers

Can standard container templates be instantiated with incomplete types?

Sometimes it's useful to instantiate a standard container with an incomplete type to obtain a recursive structure: struct multi_tree_node { // Does work in most implementations std::vector< multi_tree_node > child; }; struct trie_node { // Does…
Potatoswatter
  • 126,977
  • 21
  • 238
  • 404
17
votes
1 answer

Is Clang correct to reject code in which the nested class of a class template is defined only via specializations?

Given the following class template: template struct Outer { struct Inner; auto f(Inner) -> void; }; we define Inner separately for each specialization of Outer: template<> struct Outer::Inner {}; template<> struct…
16
votes
2 answers

Static field of an incomplete type - is it legal?

Is declaring a static field of a type that is incomplete at the moment of the class definition legal in C++? For example: Foo.h: class Foo { public: // ... private: class Bar; static Bar something; }; Foo.cpp: class Foo::Bar { //…
user784668
1
2 3
20 21