In my application, I use ChaiScript as my language for configuration files. I provide several predefined variables to the user, so he can overwrite their values to configure my program. Some of these variables have complex types and are created by the user with helper functions. Given they have polymorphic behavior, they are registered as shared pointers. Some of the variables have to be set by the user, therefore the shared pointers are initialized with
nullptr to be later substituted. Here it’s an example of what a user would write in my configuration files:
domain = discrete_domain([ "1", "2", "3", "4", "5", "6" ])
domain has type
discrete_domain is a helper function to create a
DiscreteDomain, one of the subclasses of
My problem is: in ChaiScript, variables are converted behind the scenes to make concrete objects,
shared_ptr, references, raw pointers, etc. all be used indistinguishably. By default,
operator= receives the assigned and assignee as references. As
domain is initialized as a
nullptr, when the
shared_ptr is converted I get undefined behavior.
Is there a good way to avoid this problem? When my
shared_ptrs are inside some container, everything works fine as ChaiScript doesn’t convert the template parameter. I thought about creating a wrapper
Box<T> just to avoid the conversion, but I don’t think it’s an elegant solution. After all, this is a wider question: it’s related to the way ChaiScript deals with
nullptr when implicitly converting types.