Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding templated helper function - static members or unnamed namespace

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

like image 763
Phil Rosenberg Avatar asked Nov 17 '11 13:11

Phil Rosenberg


2 Answers

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);
    }
}
like image 90
ronag Avatar answered Sep 19 '22 10:09

ronag


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).

like image 27
thiton Avatar answered Sep 19 '22 10:09

thiton