cli icon indicating copy to clipboard operation
cli copied to clipboard

`std::unique_ptr` of forward-declared classes error on libc++

Open Tomcat-42 opened this issue 1 year ago • 0 comments

Hello y'all, first of all awesome work!!!

I'm using cli in my libc++/clang project and I'm getting the error:

/usr/bin/../include/c++/v1/__memory/unique_ptr.h:66:19: error: invalid application of 'sizeof' to an incomplete type 'c
li::Menu'                                                                                                              
   66 |     static_assert(sizeof(_Tp) >= 0, "cannot delete an incomplete type");                                       
      |                   ^~~~~~~~~~~                                                                                  
/usr/bin/../include/c++/v1/__memory/unique_ptr.h:280:7: note: in instantiation of member function 'std::default_delete<
cli::Menu>::operator()' requested here                                                                                 
  280 |       __ptr_.second()(__tmp);                                                                                  
      |       ^                                                                                                        
/usr/bin/../include/c++/v1/__memory/unique_ptr.h:249:71: note: in instantiation of member function 'std::unique_ptr<cli
::Menu>::reset' requested here                                                                                         
  249 |   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 ~unique_ptr() { reset(); }                               
      |                                                                       ^                                        
/home/pablo/dev/udb/build/_deps/cli-src/include/cli/cli.h:139:9: note: in instantiation of member function 'std::unique
_ptr<cli::Menu>::~unique_ptr' requested here                                                                           
  139 |         Cli(std::unique_ptr<Menu> &_rootMenu, std::unique_ptr<HistoryStorage> historyStorage = std::make_unique
<VolatileHistoryStorage>()) :                                                                                          
      |         ^                                                                                                      
/home/pablo/dev/udb/build/_deps/cli-src/include/cli/cli.h:112:11: note: forward declaration of 'cli::Menu'             
  112 |     class Menu;                                                                                                
      |           ^                                                                                                    
1 error generated.                                                                                                     
ninja: build stopped: subcommand failed.  

It is cause by the use of std::unique_ptr<Menu> in places that Menu is forward declared. Note that using smart pointers with incomplete types is not by itself illegal, but things like calling its destructors are (like pointed by this SO issue).

I see two possible fixes:

  • Remove the forward declarations altogether by refactoring the classes in self contained headers [very hard, if not impossible considering the header-only nature of the lib]
  • Use an alternative to std::unique_ptr like a raw pointer [need to discuss the correct lifetime and ownership of the Menu objects]

I would like to discuss with you the best path for fixing this issue.

Tomcat-42 avatar Sep 13 '24 13:09 Tomcat-42