c++ -类似于python中的关键字

C++ - something like with keyword in python

本文关键字:关键字 python 类似于 c++      更新时间:2023-10-16

我有一些关于opengl着色器的抽象,我想这样使用它们:

WITH_SHADER(shader_name) {
 // here will be gl commands
}

它应该自动绑定/取消绑定当前gl上下文的着色器。在复合语句之前绑定,之后解绑定。

我可以在c++中构建这个宏吗?

最接近的是RAII。

构建一个类WithShader封装你的着色器:

  • 在构造器中绑定着色器
  • 在析构函数
  • 中取消绑定

使用例子:

{
  with_shader ws(shader_name)
  // use your shader
}
// binding and unbinding occured automatically, thats RAII.

注意:

一般来说,

RAII不是微不足道的,要注意复制和赋值构造函数