3

考虑以下代码:

template<typename T0>
void send( const std::string& func, const T0& t0 )
{
   std::ostringstream s;
   s << func << ": " << t0;
   sendMessage( s.str() );
}

template<typename T0, typename T1>
void send( const std::string& func, const T0& t0, const T1& t1 )
{
   std::ostringstream s;
   s << func << ": " << t0 << "," << t1;
   sendMessage( s.str() );
}

template<typename T0, typename T1, typename T2>
void send( const std::string& func, const T0& t0, const T1& t1, const T2& t2 )
{
   std::ostringstream s;
   s << func << ": " << t0 << "," << t1 << "," << t2;
   sendMessage( s.str() );
}

ETC...

我最多需要 15 个参数。

我只是想知道是否可以使用 Boost.Preprocessor 来简化这种重复,以及我将如何去做。我浏览了文档,但它相当混乱。

4

2 回答 2

5

boost.preprocessor可以做这种事情,你可以在这里获得下降教程:预处理元编程简介

对于您的具体示例,这是我提出的代码(注意:我只是在预处理器下运行它以检查生成的代码是否符合我的预期,但没有尝试编译代码本身)。

这既使用了enum_params有助于生成属性列表的宏,也使用local_iterate了允许使用一系列参数扩展宏的宏。如果使用“:”vs“,”处理您的特殊情况,我还有一个宏,用于发送到您的流的第一个参数。

总体而言,当您开始了解如何在其参考手册boost.preprocessor中搜索所需功能时变得相对容易,并且在大多数编译器支持可变参数模板之前,它是一个“合理的”替代品。请注意,它在编译时非常密集,因此请谨慎使用。

编辑:虽然我确实认为这是一个相对简单的示例的一般练习,但我确实同意,尽管使用 synek317 建议的伪流类对这个特定示例进行编码将是一个更灵活和“轻量级”的解决方案。再次实现此类流不需要您对所有运算符进行专门化,实际上 boost 再次提供 IOStreams 库来帮助您将类实现为标准 C++ 流(http://www.boost.org/doc/libs /1_52_0/libs/iostreams/doc/index.html )

#include <boost/preprocessor/iteration/local.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/control/if.hpp>

# ifndef MY_SEND_MAX
#  define MY_SEND_MAX 15
# endif

/*
 * macros called through BOOST_PP_REPEAT to generate lists
 * J : I don't know what it is
 * I : the current counter number
 * D : the 3rd argument of BOOST_PP_REPEAT
 */
// Generate the list of arguments after std::string
# define MY_SEND_PARAM(J,I,D) , BOOST_PP_CAT(const T,I) & BOOST_PP_CAT(arg,I)
// Generate the stream's <<s adter the string
# define MY_SEND_STREAM(J,I,D) <<BOOST_PP_IF(I,",",": ")<<BOOST_PP_CAT(arg,I)

/*
 * Macro to call for generating onde function
 * N : the number of arguments
 */
// Create one function with a string and N extra aarguments
# define MY_SEND(N) \           
  template<BOOST_PP_ENUM_PARAMS(N,typename T)>  \
  void send(std::string const &fn BOOST_PP_REPEAT(N,MY_SEND_PARAM,~)) { \
    std::ostringstream s; \
    s<<fn BOOST_PP_REPEAT(N,MY_SEND_STREAM,~); \
    sendMessage(s.str()); \
  }
// End of my macro

/*
 * BOOST_PP local loop to generate all the the function
 */
// What macro to call in the loop
#define BOOST_PP_LOCAL_MACRO(n)   MY_SEND(n)
// do the job form 0 to MY_SEND_MAX 
#define BOOST_PP_LOCAL_LIMITS     (0,MY_SEND_MAX)
// execute the loop
#include BOOST_PP_LOCAL_ITERATE()

// undefine all my stuff
# undef MY_SEND_PARAM
# undef MY_SEND_STREAM
# undef MY_SEND
# undef BOOST_PP_LOCAL_MACRO
# undef BOOST_PP_LOCAL_LIMITS
于 2013-01-29T23:50:02.790 回答
1

15 个论点听起来真是个坏主意。也许您可以创建一个行为类似于流的对象?在您的示例中,您可以轻松地从 ostringstream 继承并添加方法 send() ,我相信可以通过这种方式解决类似的问题,也许使用 operator<< 重载。

预处理器很好,但通常很难调试。

于 2012-08-30T01:38:42.207 回答