How to hide an implementation helper template?

后端 未结 5 2247
故里飘歌
故里飘歌 2020-12-15 05:21

Suppose that I have two template functions declared in a header file:

template  void func1(const T& value);
template          


        
5条回答
  •  北海茫月
    2020-12-15 05:43

    Since the user of your code needs to see the full definition of the func1 function, it's implementation, nor it's helper function implementation, can be hidden.

    But if you move the implementation into another file, the user will only have to be confronted with the template declaration:

    //templates.h
    template< typename T > void f1( T& );
    
    #include  // post-inclusion
    

    And the definition:

    // templates_impl.h
    template< typename T > void f1_helper( T& ) {
    }
    
    template< typename T > void f1( T& ) {
       // the function body
    }
    

提交回复
热议问题