mardi 4 juillet 2023

Explain implementation of function call operator in boost value_factory

I am trying to understand the boost implementation of factory design pattern. Boost provides two types of factory one for pointer types and other for value semantics. I am able to understand a little about the template value_factory class. However I am facing difficulty is understanding how operator()(..) is defined outside class and namespace.

Below is the link to complete code: https://github.com/boostorg/functional/blob/7516442815900430cc9c4a6190354e11bcbe72dd/include/boost/functional/value_factory.hpp

Code snippet after removing lot of includes.

#   ifndef BOOST_PP_IS_ITERATING
namespace boost
{
    template< typename T >  class value_factory;

    template< typename T >
    class value_factory
    {
      public:
        typedef T result_type;
        value_factory()
        { }
    }; // value_factory
    
    template< typename T > class value_factory<T&>;
} // namespace boost
#   else // defined(BOOST_PP_IS_ITERATING)
    template< BOOST_PP_ENUM_PARAMS(0, typename T) >
    inline result_type operator()(BOOST_PP_ENUM_BINARY_PARAMS(0, T, &a)) const
    {
        return result_type(BOOST_PP_ENUM_PARAMS(N,a));
    }
#   endif // defined(BOOST_PP_IS_ITERATING)

This code is from boost version 1.71.

I would like to understand how operator()(...) plays role here

  1. It is outside class
  2. It is defined in 2nd conditional block # else // defined(BOOST_PP_IS_ITERATING)

As per my understanding member function operator()(...) should have been inside the class.

Aucun commentaire:

Enregistrer un commentaire