标签: boost-preprocessor

使用 Boost.Preprocessor 减少此代码重复

考虑以下代码:

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 …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-preprocessor

3
推荐指数
1
解决办法
3000
查看次数

在编译时生成BitCount LUT

假设我需要为0 ... 255值创建一个包含预先计算的位计数值(数字中的1位数)的LUT:

int CB_LUT[256] = {0, 1, 1, 2, ... 7, 8};
Run Code Online (Sandbox Code Playgroud)

如果我不想使用硬编码值,我可以使用漂亮的模板解决方案如何计算32位整数中的设置位数?

template <int BITS>
int CountBits(int val) 
{
    return (val & 0x1) + CountBits<BITS-1>(val >> 1);
}

template<>
int CountBits<1>(int val) 
{
    return val & 0x1;
}

int CB_LUT[256] = {CountBits<8>(0), CountBits<8>(1) ... CountBits<8>(255)}; 
Run Code Online (Sandbox Code Playgroud)

该数组在编译时完全计算.有没有办法避免长列表,并使用某种模板甚至宏生成这样的数组(抱歉!),如:

Generate(CB_LUT, 0, 255);  // array declaration
...
cout << CB_LUT[255];       // should print 8
Run Code Online (Sandbox Code Playgroud)

笔记.这个问题不是关于计算一个数字中的1位,而是仅用作示例.我想在代码中完全生成这样的数组,而不使用外部代码生成器.必须在编译时生成数组.

编辑.为了克服编译器限制,我找到了以下解决方案,基于Bartek Banachewicz代码:

#define MACRO(z,n,text) CountBits<8>(n)
int CB_LUT[] = {
    BOOST_PP_ENUM(128, MACRO, _)
};
#undef …
Run Code Online (Sandbox Code Playgroud)

c++ templates boost boost-preprocessor

3
推荐指数
1
解决办法
415
查看次数

C++通用编程的细微之处

我遇到的问题在以下代码中说明.

#include <iostream>

#define X 4

int main()
{

    std::cout << "should be 4: " << X << std::endl;
#define Y X + 4
    std::cout << "should be 8: " << Y << std::endl;

#undef Y
#define Y X+0
#undef X
#define X Y+1

    std::cout << "expecting 5: " << X << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

错误:

test2.cc: In function ‘int main()’:
test2.cc:17: error: ‘X’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

我试图模仿的模式是在代码/构建级别扩展程序(很像nginx模块在编译时如何连接).我需要构建一个可扩展的编译时结构,它可以通过向#include我的构建中添加s来进行扩展(可插入),从而生成一个boost-mpl-vector,其中包含一个包含所有插件的唯一名称.因此,如果X是唯一的结束名称,则X_0,X_1,X_2是沿着向量 …

c++ boost-mpl boost-preprocessor preprocessor-meta-program

2
推荐指数
1
解决办法
281
查看次数

如何使用boost预处理器来运行一系列函数?

例如,我有一系列函数f1,f2等,具有相同的两个参数类型.我想用宏

RUN((f1)(f2)(f3), a, b)
Run Code Online (Sandbox Code Playgroud)

用结果运行函数序列

f1(a, b), f2(a, b), f3(a, b)
Run Code Online (Sandbox Code Playgroud)

我认为升压预处理器可以提供帮助.我试过了

#define RUN_DETAIL(pR, pData, pF) pF(a, b);
#define RUN(pFs, a, b) BOOST_PP_SEQ_FOR_EACH(RUN_DETAIL, BOOST_PP_EMPTY, pFs)
Run Code Online (Sandbox Code Playgroud)

但失败了.怎么做?

找到答案如下

#define RUN_DETAIL(pR, pData, pF) pF pData;
#define RUN(pFs, ...) BOOST_PP_SEQ_FOR_EACH(RUN_DETAIL, (__VA_ARGS__), pFs)
Run Code Online (Sandbox Code Playgroud)

此技术也适用于调用宏序列.

c++ boost boost-preprocessor

2
推荐指数
1
解决办法
451
查看次数

强制预处理器在重定义中使用先前的定义

更新3:

没关系.我有点得到我想要的东西.以下内容给出了类中的唯一标识符.

static const int _counter_start = __COUNTER__;
static const int val1 = __COUNTER__ - _counter_start;
static const int val2 = __COUNTER__ - _counter_start;
Run Code Online (Sandbox Code Playgroud)

更新2:

提升预处理器

我将使用此功能实现类似于消息映射的内容.

class a
{
...
    MAP_BEGIN()
    MAP_DECL...
    MAP_END()
...
};
Run Code Online (Sandbox Code Playgroud)

问题是,对于每个MAP_DECL,我需要在两个地方扩展宏.

class a
{    
    virtual void func()
    {        
        ...
        //does something with the decl declaration        
    }
    ...
    //also expand some stuff here    
}
Run Code Online (Sandbox Code Playgroud)

Boost预处理器应该(理论上)允许我将MAP_DECL累积到一个序列中并在最后将它扩展为func()(同时在我们去的同时扩展类字段).


更新1:

我目前正在使用Boost预处理器库.我每次需要在序列中添加一些内容时,我目前无法创建一个新的宏变量/定义,如下所示.

我正在尝试扩展Boost预处理器序列,而我现在仍然坚持这样做

#define SEQ (w)(x)(y)(z) 
#define SEQ2 BOOST_PP_SEQ_PUSH_BACK(SEQ, a)
Run Code Online (Sandbox Code Playgroud)

原文:

假设我有以下代码

#define CUR 2
#define …
Run Code Online (Sandbox Code Playgroud)

c++ g++ c-preprocessor boost-preprocessor

0
推荐指数
1
解决办法
1699
查看次数