简体   繁体   English

C++ 模板预处理器工具

[英]C++ Template preprocessor tool

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?是否有编译器或独立预处理器采用 C++ 文件并运行模板扩展过程,生成带有扩展模板实例化的新 C++ 代码?

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.我记得在 20 世纪 90 年代中期有这样一个工具,当时模板还是新的和实验性的,预处理器是一种在没有本机模板支持的情况下使用编译器进行模板编程的方法。

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 代码时使用这样的工具。 OpenCL is C++, but does not support templates. OpenCL为 C++,但不支持模板。 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.我希望我可以编写模板,即使是简单的模板,例如 integer 或 bool only arguments,并让一些工具预解析文件和 go 并找到模板的用途并扩展调用并给我新的 C++ 代码OpenCL 编译器可以理解。

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.替代方案: #define macros everywhere..更丑陋,不安全,效率低下,通用性差。

Comeau C ++可以将C ++ “编译”为C.这似乎接近你的目标,因为OpenCL不支持C ++ - 它更接近于C.

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

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;
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM