stclib / STC

A modern, user friendly, generic, type-safe and fast C99 container library: String, Vector, Sorted and Unordered Map and Set, Deque, Forward List, Smart Pointers, Bitset and Random numbers.
MIT License
1.35k stars 73 forks source link

Unable to forward decalre incomplete type for stack #64

Closed krumlo closed 1 year ago

krumlo commented 1 year ago

Following the README I've written this code

typedef struct Point Point;

forward_cstack(cstack_pnt, Point); // declare cstack_pnt (and cstack_pnt_value, cstack_pnt_iter);
                                          // struct Point may be an incomplete type.
typedef struct Dataset {
    cstack_pnt vertices;
    cstack_pnt colors;
} Dataset;

// Implementation
#define i_is_forward                  // flag that the container was forward declared.
#define i_key Point
#define i_tag pnt
#include <stc/cstack.h>

But when I'm building I get loads of errors due to incomplete type, e.g

In file included from /home/dankru/repos/cge/src/cge/layers/layer_stack.c:28:
/home/dankru/repos/cge/vendor/STC/include/stc/cstack.h:62:48: error: invalid application of 'sizeof' to an incomplete type 'Point' (aka 'struct Point')
    _cx_self out = {(_cx_value *) i_malloc(cap*c_sizeof(i_key)), 0, cap};
                                  ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
/home/dankru/repos/cge/vendor/STC/include/stc/ccommon.h:89:43: note: expanded from macro 'c_sizeof'
#define c_sizeof                (intptr_t)sizeof
                                          ^
/home/dankru/repos/cge/vendor/STC/include/stc/ccommon.h:78:46: note: expanded from macro 'c_malloc'
#define c_malloc(sz)            malloc(c_i2u(sz))
                                       ~~~~~~^~~
/home/dankru/repos/cge/vendor/STC/include/stc/ccommon.h:98:43: note: expanded from macro 'c_i2u'
#define c_i2u(i)                ((size_t)(i) + 0*sizeof((i) == 1))
                                          ^
/home/dankru/repos/cge/src/cge/layers/layer_stack.c:15:16: note: forward declaration of 'struct Point'
typedef struct Point Point;
               ^
In file included from /home/dankru/repos/cge/src/cge/layers/layer_stack.c:28:
/home/dankru/repos/cge/vendor/STC/include/stc/cstack.h:62:48: error: invalid application of 'sizeof' to an incomplete type 'Point' (aka 'struct Point')
    _cx_self out = {(_cx_value *) i_malloc(cap*c_sizeof(i_key)), 0, cap};
                                  ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
/home/dankru/repos/cge/vendor/STC/include/stc/ccommon.h:89:43: note: expanded from macro 'c_sizeof'
#define c_sizeof                (intptr_t)sizeof
                                          ^
/home/dankru/repos/cge/vendor/STC/include/stc/ccommon.h:78:46: note: expanded from macro 'c_malloc'
#define c_malloc(sz)            malloc(c_i2u(sz))
                                       ~~~~~~^~~
/home/dankru/repos/cge/vendor/STC/include/stc/ccommon.h:98:58: note: expanded from macro 'c_i2u'
#define c_i2u(i)                ((size_t)(i) + 0*sizeof((i) == 1))
                                                         ^
/home/dankru/repos/cge/src/cge/layers/layer_stack.c:15:16: note: forward declaration of 'struct Point'
typedef struct Point Point;
               ^
In file included from /home/dankru/repos/cge/src/cge/layers/layer_stack.c:28:
/home/dankru/repos/cge/vendor/STC/include/stc/cstack.h:66:63: error: variable has incomplete type 'Point' (aka 'struct Point')
STC_INLINE _cx_self _cx_memb(_with_size)(intptr_t size, i_key null) {
                                                              ^
/home/dankru/repos/cge/src/cge/layers/layer_stack.c:15:16: note: forward declaration of 'struct Point'
typedef struct Point Point;

What am I missing? :)

tylov commented 1 year ago

You need to #include <stc/forward.h> before the forward_cstack() declaration. And you must define struct Point in the implementation (c file), before #include <stc/cstack.h>. I will update the docs on this, thanks.