C++ - something like with keyword in python

c++, python

Solution

The closest thing would be RAII.

Build a class `WithShader` that encapsulates your shader :

- Bind the shader in the constructor

- Unbind it in the destructor

Usage example:

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

Note:

RAII is not trivial in general, pay attention to the copy and assignment constructors

Problem

Well i have some abstraction around opengl shaders and i want to use them this way: ``` WITH_SHADER(shader_name) { // here will be gl commands } ``` it should automatically bind/unbind that shader from current gl context. Bind before that compound statement and unbind after that. Can i construct this macro in C++ somehow?

Original source