In a header-only library, I need to run some initialization code before using any library functionality. At the moment, I have an explicit init()
function for this purpose that the user needs to call explicitly.
I would like to avoid forcing the user to remember to call such a function. A partial solution is to embed the init logic in an object constructor, and then store an instance of such object as static member in a class template:
struct initializer {
initializer()
{
// init() logic here.
}
};
template <typename = void>
struct holder {
static initializer s_init;
};
template <typename T>
initializer holder<T>::s_init;
The problem with such a solution is that, in order for the s_init
member to be actually constructed at runtime, the holder
class template needs to be instantiated.
My question then is this: is it enough to define an inline
function for the purpose of triggering the instantiation of the holder
object? I.e., something like
inline void trigger_instantiation()
{
auto init = holder<>::s_init;
}
to be included in all the headers of the library?
Aucun commentaire:
Enregistrer un commentaire