C++ 模板预处理器工具

发布于 2024-07-27 21:46:42 字数 536 浏览 4 评论 0原文

是否有一个编译器或独立预处理器可以获取 C++ 文件并运行模板扩展过程,从而生成具有扩展模板实例化的新 C++ 代码?

我记得在 90 年代中期就有这样的工具,当时模板还很新,还处于实验阶段,预处理器是一种在没有本机模板支持的情况下使用编译器进行模板编程的方法。

这比宏处理步骤复杂得多,因为它可能需要解析和标记代码以理解上下文。

我希望在编写OpenCL代码时使用这样的工具。 OpenCL 是 C++,但不支持模板。 我希望我可以编写模板,甚至是简单的模板,例如仅使用整数或布尔参数,并使用一些工具来预解析文件并浏览并找到模板的使用并扩展调用,并为我提供新的 C++ 代码OpenCL 编译器可以理解。

即使是非常有限的工具也可能有用,它不需要支持每个模板怪癖,甚至不需要支持多个模块或任何东西。

另一种选择:#define 宏随处可见……更丑陋、不安全、效率较低、通用性较差。

Is there a compiler or standalone preprocessor which takes C++ files and runs a template expansion pass, generating new C++ code with expanded template instantiations?

I remember such a tool in the mid-90s when templates were still new and experimental, and the preprocessor was a way to do template programming with compilers without native template support.

This is a lot more complicated than a macro-processing step since it would likely require parsing and tokenizing the code to understand the contexts.

My hope is to use such a tool when writing OpenCL code. OpenCL is C++, but does not support templates. I'm hoping I can write templates, even simple ones like with integer or bool only arguments, and have some tool pre-parse the file and go through and find the use of the templates and expand the invocations and give me new C++ code that the OpenCL compiler can understand.

Even a very limited tool could be useful, it does not need to support every template quirk, nor even support multiple modules or anything.

The alternative: #define macros everywhere.. uglier, unsafe, less efficient, and less versatile.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春花秋月 2024-08-03 21:46:42

Comeau C++ 可以将 C++“编译”为 C。这似乎接近您的目标,因为 OpenCL 没有支持 C++——它更接近 C。

Comeau C++ can "compile" C++ to C. This would seem to be close to your goal, as OpenCL does not support C++ – it's much closer to C.

甜中书 2024-08-03 21:46:42

C++ Insights (https://cppinsights.io/) 能够做到这一点(更普遍地扩展“高-level”语法糖 C++ 构造。它基于 Clang,因此它对代码有尽可能好的理解,并支持最新标准。

例如,它将扩展

extern "C" void printf(...);
template<typename T>
int foo(T t)
{
  if constexpr(sizeof(T) == 4) {
    printf("int: %d", t);
  }
  else {
    printf("something else: %d", (int)t);
  }
}

int main()
{
    const char arr[10]{2,4,6,8};

    for(const char& c : arr)
    {
      foo(c);
    }
}

extern "C" void printf(...);
template<typename T>
int foo(T t)
{
  if constexpr(sizeof(T) == 4) {
    printf("int: %d", t);
  }
  else {
    printf("something else: %d", (int)t);
  }
}

/* First instantiated from: insights.cpp:19 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
int foo<char>(char t)
{
  if constexpr(false) {
  } else /* constexpr */ {
    printf("something else: %d", static_cast<int>(t));
  } 
  
}
#endif


int main()
{
  const char arr[10] = {2, 4, 6, 8, '\0', '\0', '\0', '\0', '\0', '\0'};
  {
    char const (&__range1)[10] = arr;
    const char * __begin1 = __range1;
    const char * __end1 = __range1 + 10L;
    for(; __begin1 != __end1; ++__begin1) {
      const char & c = *__begin1;
      foo(c);
    }
    
  }
  return 0;
}

C++ Insights (https://cppinsights.io/) is able to do this (and more generally expansion of "high-level" syntax-sugary C++ constructs. It is based on Clang so it has an understanding of the code which is as good as possible, and supports latest standards.

It will for instance expand

extern "C" void printf(...);
template<typename T>
int foo(T t)
{
  if constexpr(sizeof(T) == 4) {
    printf("int: %d", t);
  }
  else {
    printf("something else: %d", (int)t);
  }
}

int main()
{
    const char arr[10]{2,4,6,8};

    for(const char& c : arr)
    {
      foo(c);
    }
}

into

extern "C" void printf(...);
template<typename T>
int foo(T t)
{
  if constexpr(sizeof(T) == 4) {
    printf("int: %d", t);
  }
  else {
    printf("something else: %d", (int)t);
  }
}

/* First instantiated from: insights.cpp:19 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
int foo<char>(char t)
{
  if constexpr(false) {
  } else /* constexpr */ {
    printf("something else: %d", static_cast<int>(t));
  } 
  
}
#endif


int main()
{
  const char arr[10] = {2, 4, 6, 8, '\0', '\0', '\0', '\0', '\0', '\0'};
  {
    char const (&__range1)[10] = arr;
    const char * __begin1 = __range1;
    const char * __end1 = __range1 + 10L;
    for(; __begin1 != __end1; ++__begin1) {
      const char & c = *__begin1;
      foo(c);
    }
    
  }
  return 0;
}
呆° 2024-08-03 21:46:42

没有这样的工具 - 模板是语言的一部分,而不是一些预处理器通道 - 它们由编译器处理,就像其他代码一样。

There is no such tool - templates are part of the language, not some pre-processor pass - they are processed by the compiler, just like other code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文