How to explicitly instantiate a template for all members of MPL vector in C++?

后端 未结 6 570
日久生厌
日久生厌 2021-02-01 05:53

Consider the following header file:

// Foo.h
class Foo {
    public: 
        template 
        void read(T& value);
};

I

6条回答
  •  梦毁少年i
    2021-02-01 06:33

    I am not sure if this is the solution to your problem, but maybe you can do with a template specialization.

    New header:

    // Foo.h
    
    template < typename T >
    struct RealRead;
    
    class Foo {
        public: 
            template 
            void read(T& value);
    };
    
    template 
    void Foo::read(T& value)
    {
      RealRead< T >::read( value );
    }
    

    New source :

    template < >
    struct RealRead< int >
    {
      static void read( int & v )
      {
        // do read
      }
    };
    template < >
    struct RealRead< float >
    {
      static void read( float & v )
      {
        // do read
      }
    };
    
    //etc
    
    // explicitly instantiate templates
    template struct RealRead< int >;
    template struct RealRead< float >;
    

提交回复
热议问题