I'm trying to write a library where I have some templated functions, some of which are helper functions so I don't want my users to have access to them. Some basic code might be
//mylib.h
namespace myfuncs
{
template<class T>
void helper (T input, int extrainformation)
{
//do some usefull things
}
template<class T>
void dostuff(T input)
{
int someinfo=4;
helper(input, someinfo);
}
}
Is it possible to somehow hide the helper function so that users of the library can't call it directly? I had thought an unnamed namespace might do the job but because I'm using templates I can't split the function declaration and body between a header and implementation file. Putting the unnamed namespace in a header file is of no use and bad style. The only thing I can think to do is create a mylib
class and encapsulate the functions as private/public static functions.
Any better solutions would be much appreciated.
Phil
One way to do it is to have a "detail" or "internal" namespace. Thats how many libraries do it.
namespace myfuncs
{
namespace detail
{
template<class T>
void helper (T input, int extrainformation)
{
//do some usefull things
}
}
template<class T>
void dostuff(T input)
{
int someinfo=4;
detail::helper(input, someinfo);
}
}
Do what many template libraries (like Eigen) do: use a clearly named implementation-specific namespace (such as myfuncs::impl
) and rely on social encapsulation (i.e. the user not willing to call templates from the implementation namespace).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With