[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

C++ question on wrapper API for setting Guile fluids

From: Jean Abou Samra
Subject: C++ question on wrapper API for setting Guile fluids
Date: Thu, 21 Apr 2022 00:04:26 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0


It's me again ...

I am working on code that pervasively utilizes Guile fluids. They should
be set in dynamic scopes. In C++, usage of the Guile API looks like this:

  scm_dynwind_begin ();
  scm_dynwind_fluid (fluid, value);
  scm_dynwind_end ();


Calls to scm_dynwind_begin () and scm_dynwind_end () must be
paired correctly. Obviously, if '...' contains statements
causing non-local control flow like return, break, continue,
and such, it is easy to miss a scm_dynwind_end (). I read
that you can avoid that using RAII:

    // Constructor does scm_dynwind_begin () and scm_dynwind_fluid ()
    Fluid_setter setter (fluid, value);
    // Rest of this block is in a dynamic scope where the fluid
    // has the new value.
    // Destructor does scm_dynwind_end ().

What disturbs me slightly with this approach is the variable 'setter',
which will never be used, but must nevertheless be defined in order
for the object not to be thrown away as temporary.

Does that sound about right? Would this be considered an idiomatic

An alternative would be

  call_with_fluid (fluid, value, [&](){

However, this does not map in the same straightforward way to fluid-less
code: it does not allow return statements jumping out of the main function
(as opposed to the inner lambda).

Does that nevertheless look better to trained C++ eyes? Are there
other ways?

Thanks for your patience,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]