标签: boost-preprocessor

如何在编译时显示#define的值?

我试图找出我的代码认为它正在使用的Boost版本.我想做这样的事情:

#error BOOST_VERSION

但预处理器不会扩展BOOST_VERSION.

我知道我可以在程序运行时打印出来,我知道我可以查看预处理器的输出来找到答案.我觉得在编译期间有一种方法可以使用它.

macros c-preprocessor boost-preprocessor

110
推荐指数
7
解决办法
10万
查看次数

C99预处理器图灵是否完整?

在发现Boost预处理器的功能后,我发现自己在想:C99预处理器Turing是否完整?

如果没有,缺少什么不符合资格?

theory turing-complete c-preprocessor boost-preprocessor

67
推荐指数
4
解决办法
1万
查看次数

带有Boost的D_WIN32_WINNT编译器警告

不知道该怎么做这个错误.添加-D_WIN32_WINNT=0x0501到项目属性下的Visual Studio的"命令行"选项,但它表示它无法识别它并且警告仍然出现.

我也不确定如何添加预处理器定义.

1>请正确定义_WIN32_WINNT或_WIN32_WINDOWS.例如:
1> - 将-D_WIN32_WINNT = 0x0501添加到编译器命令行; 或
1> - 将_WIN32_WINNT = 0x0501添加到项目的预处理器定义中.

c++ boost boost-preprocessor

24
推荐指数
3
解决办法
2万
查看次数

Boost是否使用合法的C++预处理指令语法?

我的(相对较旧的)C++编译器在Boost中对此文件进行了阻塞,起始时间为:

# /* Copyright (C) 2001
#  * Housemarque Oy
#  * http://www.housemarque.com
#  *
#  * Distributed under the Boost Software License, Version 1.0. (See
#  * accompanying file LICENSE_1_0.txt or copy at
#  * http://www.boost.org/LICENSE_1_0.txt)
#  */
#
Run Code Online (Sandbox Code Playgroud)

这真的是合法的C++吗?预处理器令牌的语法规则是什么?

c++ syntax boost c-preprocessor boost-preprocessor

13
推荐指数
1
解决办法
349
查看次数

用宏构造#include指令的路径

我想为宏程序动态创建的包含文件路径,用于程序的目标配置相关部分.

例如,我想构建一个可以像这样调用的宏:

#include TARGET_PATH_OF(header.h)
Run Code Online (Sandbox Code Playgroud)

这会扩展到这样的事情:

#include "corefoundation/header.h"
Run Code Online (Sandbox Code Playgroud)

当为OSX配置源(在本例中)时

到目前为止,所有尝试都失败了 我希望以前有人这样做过吗?

什么不起作用的例子:

#include <iostream>
#include <boost/preprocessor.hpp>

#define Dir directory/
#define File filename.h

#define MakePath(f) BOOST_PP_STRINGIZE(BOOST_PP_CAT(Dir,f))
#define MyPath MakePath(File)

using namespace std;

int main() {
    // this is a test - yes I know I could just concatenate strings here
    // but that is not the case for #include
    cout << MyPath << endl;
}
Run Code Online (Sandbox Code Playgroud)

错误:

./enableif.cpp:31:13: error: pasting formed '/filename', an invalid preprocessing token
    cout << MyPath << endl;
            ^
./enableif.cpp:26:16: …
Run Code Online (Sandbox Code Playgroud)

c++ macros include boost-preprocessor

12
推荐指数
1
解决办法
4137
查看次数

我可以附加到预处理器宏吗?

在标准C或GNU扩展中有什么方法可以将内容附加到宏定义中吗? 例如,给定一个宏定义为
#define List foo bar
可以追加,bas以便它List扩展,就像我定义它一样
#define List foo bar bas

我希望我能做到这样的事情:

#define List    foo bar bas

#define List_   Expand(List)
#undef List
#define List    Expand(List_) quux
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何定义Expand()宏,所以它会做我想要的.

动机: 我在这些方面玩歧视/标记的工会:

struct quux_foo { int x; };
struct quux_bar { char *s; };
struct quux_bas { void *p; };

enum quux_type {quux_foo, quux_bar, quux_bas};

struct quux {
    enum quux_type type;
    union {
        struct quux_foo foo;
        struct quux_bar bar;
        struct quux_bas bas;
    } t; …
Run Code Online (Sandbox Code Playgroud)

c macros c-preprocessor boost-preprocessor x-macros

9
推荐指数
2
解决办法
5143
查看次数

递归显式模板实例化以导出库的符号

在我之前的问题中,我问过递归显式模板实例化是否可能.我看到它确实可能; 但是,此实例化仅在本地有效,递归实例化模板的符号不会导出到目标文件,因此不会出现在(共享)库中.所以我在这里更准确地提出问题,就像我之前的帖子一样:

给出一个类似的模板

template<int dim> class Point { ... };
Run Code Online (Sandbox Code Playgroud)

这个模板可以明确地实例化

template class Point<0>;
template class Point<1>;
template class Point<2>;
template class Point<3>;
Run Code Online (Sandbox Code Playgroud)

它将Point<0>...... 的符号导出Point<3>到当前翻译单元的目标文件中.我没有像上面那样单独实例化每个模板,而是想通过一次调用递归地实例化它们.

任何实现这一目标的解决方案都很好,无论是模板元编程的风格,还是通过辅助类

template class RecursiveInstantiate<Point, 3>;
Run Code Online (Sandbox Code Playgroud)

或通过预处理器.在这里,我研究了boost预处理器库,它似乎有一些循环结构.但是,我从未使用过boost预处理器库(任何建议都很受欢迎),但乍一看,如果循环可以与显式模板实例一起使用,我会持怀疑态度.

任何建议,也解释为什么我想要达到的目标是不可能的.


其实我感兴趣的概括这对于喜欢多个模板参数类Node<int i1,int i2,int i3>为I1,I2,I3的所有组合{0,1,2,3}.但我希望能够自己解决这第二部分.像往常一样,我想通过仅在一个转换单元中定义模板来使用显式实例化来加速编译时间,因此我需要将模板的方法导出到目标文件中.

我希望有一个独立于编译器的解决方案,但如果这不可能,我需要用于带有g ++/clang的Linux.


请参阅下文,了解我所获得的解决方案以及我从中获得的最终解决方案.

c++ templates boost-preprocessor

8
推荐指数
1
解决办法
1399
查看次数

如何计算传递给可变参数宏的宏参数的数量?

我已经大部分时间了:

#include <boost/preprocessor.hpp>
#define COUNT(...) BOOST_PP_VARIADIC_SIZE(__VA_ARGS__)
COUNT(1,2,3)
COUNT(1,2)
COUNT(1)
COUNT()
Run Code Online (Sandbox Code Playgroud)

-E在GCC中使用标志运行此输出以下内容

3 2 1 1

当我需要的是:

3 2 1 0

我在这做错了什么?我没有开始使用boost preprocessor,但我确实需要解决方案是可变的.

任何想法如何让这个工作?

c++ boost-preprocessor

8
推荐指数
1
解决办法
705
查看次数

可以实现BOOST_PP_DEFINED吗?

是否有可能编写一个类似函数的C预处理器宏,1如果定义了它的参数则返回,0否则?让我们BOOST_PP_DEFINED通过与其他boost预处理器宏类比来调用它,我们可以假设它们也在起作用:

#define BOOST_PP_DEFINED(VAR) ???

#define XXX
BOOST_PP_DEFINED(XXX)  // expands to 1
#undef XXX
BOOST_PP_DEFINED(XXX)  // expands to 0
Run Code Online (Sandbox Code Playgroud)

我期待使用的结果BOOST_PP_DEFINEDBOOST_PP_IIF:

#define MAGIC(ARG) BOOST_PP_IIF(BOOST_PP_DEFINED(ARG), CHOICE1, CHOICE2)
Run Code Online (Sandbox Code Playgroud)

换句话说,我希望扩展MAGIC(ARG)根据扩展时是否ARG定义而变化MAGIC:

#define FOO
MAGIC(FOO)  // expands to CHOICE1 (or the expansion of CHOICE1)
#undef FOO
MAGIC(FOO)  // expands to CHOICE2 (or the expansion of CHOICE2)
Run Code Online (Sandbox Code Playgroud)

我还发现以下内容不起作用(有点令人惊讶):

#define MAGIC(ARG) BOOST_PP_IIF(defined(arg), CHOICE1, CHOICE2)
Run Code Online (Sandbox Code Playgroud)

因为defined当用作#if表达式的一部分时,显然只在预处理器中有效.

我有点怀疑增强预处理器尚未提供BOOST_PP_DEFINED …

c++ macros boost c-preprocessor boost-preprocessor

6
推荐指数
1
解决办法
351
查看次数

如何在粘贴之前强制处理器使用表达式结果

我的代码是

#define PASTE__(a, b) a##b
#define PASTE_(a, b)  PASTE__(a, b)
#define PASTE(a, b) PASTE_(a, b)

int main()
{
    PASTE(1, (1+3)/4);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望结果是

int main()
{
    11;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

可编译链接:http://coliru.stacked-crooked.com/a/b35ea3e35a1b56ae

我在粘贴之前放入了如何保证参数的完整宏扩展?建议的两个间接级别。

但我仍然收到预处理器错误:

main.c:8:11: error: pasting "1" and "(" does not give a valid preprocessing token
     PASTE(1, (1+3)/4);
           ^
main.c:1:23: note: in definition of macro 'PASTE__'
 #define PASTE__(a, b) a##b
                       ^
main.c:3:21: note: in expansion of macro 'PASTE_'
 #define PASTE(a, b) PASTE_(a, b)
                     ^
main.c:8:5: note: …
Run Code Online (Sandbox Code Playgroud)

c macros c-preprocessor boost-preprocessor

5
推荐指数
1
解决办法
818
查看次数