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
15
votes
3 answers

invalid use of incomplete type / forward declaration

I tried to look at the similar problems listed here on Stackoverflow and on Google but they deal mostly with templates and that's not my case. I'm using GCC 4.4.5 on Debian Testing 64bit. So, I have two classes - CEntity: #ifndef…
rivon
  • 151
  • 1
  • 1
  • 3
14
votes
2 answers

Incomplete class usage in template

I am very surprised that on various sampled versions of g++, the following compiles without error or warning: // Adapted from boost::checked_delete() template inline void assert_complete() { typedef char type_must_be_complete[ sizeof(T)…
aschepler
  • 65,919
  • 8
  • 93
  • 144
14
votes
1 answer

incomplete types with std::map and std::variant

Consider this simplified and very specific implementation of a recursive variant on top of std::variant: #include #include struct recursive_tag; template struct RecursiveVariant; template <> struct…
Barry
  • 247,587
  • 26
  • 487
  • 819
14
votes
2 answers

Why can't "is_base_of" be used inside a class declaration (incomplete type)?

I completely see why this cannot work: class Base {}; class A; static_assert(std::is_base_of::value, ""); Because there is no information about a 'class hierarchy', but... Why cannot the following work? class Base {}; class A : public Base…
user2561762
  • 353
  • 1
  • 8
14
votes
1 answer

Why an inline declaration is not an incomplete type?

Consider the code below: struct Foo { struct Bar; Foo() { Bar bar; // Why isn't Bar an incomplete type?! } struct Bar {}; // Full definition }; // struct Bar {}; // fails to compile due to incomplete type int main() { …
vsoftco
  • 52,188
  • 7
  • 109
  • 221
14
votes
1 answer

Incomplete types in template code

Suppose we have two types (complete and incomplete): struct CompleteType{}; struct IncompleteType; Also we have template code: #include template struct Test : std::false_type {}; template <> struct Test :…
Constructor
  • 6,903
  • 2
  • 17
  • 55
12
votes
2 answers

Does T have to be a complete type to be used in `std::declval`?

Consider this example (coming from here): #include #include template struct A { }; struct B { template A f() { return A{}; } using default_return_type =…
463035818_is_not_a_number
  • 64,173
  • 8
  • 58
  • 126
12
votes
1 answer

Why does unary operator & not require a complete type?

The following code compiles fine with both gcc 7.2.0 and clang 6.0.0. #include struct stru; void func(stru& s) { std::cout << &s << std::endl; } int main() { } I'm wondering how this is OK. What if stru has overloaded operator&()?…
Lingxi
  • 13,248
  • 1
  • 32
  • 74
12
votes
4 answers

All struct identifiers are automatically forward declared

While answer warning: assignment from incompatible pointer type for linklist array, I noticed any undeclared identifier perceded with struct keyword are considered as forward declared identifiers. For instance the program below compiles well: /*…
Mohit Jain
  • 29,414
  • 8
  • 65
  • 93
12
votes
1 answer

Incomplete type for std::vector

The GCC compiler complains (see below) when I try the following. class Face needs to be incomplete because it contains pointer to class Element which similarly contains pointer to class Face. In other words, there is a circular dependency among…
Shibli
  • 5,383
  • 13
  • 54
  • 115
11
votes
7 answers

Disadvantages of forward declaration?

In C++ and Objective-C, I've gotten into the habit of forward-declaring any necessary classes that do not need to be defined in the header, and then importing the header files defining those classes in source files if needed. Is there ever a…
Luke
  • 6,780
  • 6
  • 39
  • 69
11
votes
3 answers

Initialising a struct that contains a vector of itself

I have a menu system that I want to initialise from constant data. A MenuItem can contain, as a sub-menu, a vector of MenuItems. But it only works up to a point. Here are the bare bones of the problem: #include struct S { std::vector v ;…
TonyK
  • 15,585
  • 4
  • 29
  • 65
11
votes
3 answers

Incomplete types as function parameters and return values

The following code compiles successfully both with clang++ 5.0.0 and g++ 7.2 (with the -std=c++17 -Wall -Wextra -Werror -pedantic-errors -O0 compilation flags): struct Foo; struct Bar { Foo get() const; void set(Foo); }; struct…
Constructor
  • 6,903
  • 2
  • 17
  • 55
11
votes
4 answers

In C language, is it semantically possible to create an lvalue with incomplete type?

In the C89 standard, I found the following section: 3.2.2.1 Lvalues and function designators Except when it is the operand of the sizeof operator, the unary & operator, the ++ operator, the -- operator, or the left operand of the . operator or an…
11
votes
1 answer

C++ dynamic_cast to forward declared class template compiles, but is it safe?

The following code compiles and gives result as one would expect in (GCC and clang): template struct Derived; struct Base { template void foo(T * const t) { dynamic_cast *…
ThomasMcLeod
  • 7,026
  • 4
  • 37
  • 71
1
2
3
20 21