标签: preprocessor-meta-program

C++预处理器元编程图灵完备吗?

我知道C++模板元编程是Turing-complete.预处理程序元编程是否同样适用?

c++ metaprogramming turing-complete c-preprocessor preprocessor-meta-program

5
推荐指数
2
解决办法
2277
查看次数

如何在预处理阶段获取c ++函数调用者名称

我必须使用PERF_INSTRUMENT库中的宏.PERF_INSTRUMENT期望用户提供c样式字符串作为函数名称来打印此仪器点的位置.

但是,我不想每次使用时编写函数名称,PERF_INSTRUMENT而是想要调用它,__func__ 以便函数名称自动包含在perf日志中.

但是当我使用__func__它时实际返回operator()是因为__func__它嵌入在lambda函数中.

他们可以通过哪种方式将main()函数名称传递给PERF_INSTRUMENT宏.

#include <cstdio>
#include <cassert> 
#include <type_traits> 

using namespace std;

namespace /* anonymous */
{
    template< typename T >
    struct Is_Const_Char_Array
      : std::is_same< std::remove_reference_t< T >,
                      char const[ std::extent< std::remove_reference_t< T > >::value ] >
    {};

    template< typename T >
    struct Is_C_String_Literal
      : Is_Const_Char_Array< T >
    {};
}

#define PERF_INSTRUMENT(name)  auto instObj = [] { static_assert( Is_C_String_Literal< decltype( name …
Run Code Online (Sandbox Code Playgroud)

c++ template-meta-programming c-preprocessor preprocessor-meta-program c++11

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

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
查看次数

我可以使用C++宏将代码插入到不同的地方吗?

我想编写一个宏来声明一个结构体字段并将该字段的初始化过程插入到一个Init()函数中。但我发现宏不可能将代码插入到不同的位置(在本例中为字段声明和 Init() 函数定义)。

// I have 
template <typename T>
T* initValue() {
  // return T* or nullptr
}

// I need some sort of 
DECL_CLASS(MyClass)
DECL_FIELD(int, foo)
DECL_FIELD(float, bar)
DECL_CLASS_END()

// To generate
struct MyClass {
  int *foo;
  float *bar;

public:
  bool Init() {
    if (!(foo = initValue<decay_t<decltype(*foo)>>())) {
      return false;
    }
    if (!(bar = initValue<decay_t<decltype(*bar)>>())) {
      return false;
    }
    return true;
  }
};
Run Code Online (Sandbox Code Playgroud)

我可以使用某种代码生成工具(例如 cog)来实现此目的,但我不想在代码库中引入额外的语法。我可以用纯 C++ 实现这个吗?

如果预处理器在这种情况下无法提供帮助,您将如何使该结构定义更简单?

c++ template-meta-programming preprocessor-meta-program

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