'A more elegant way of writing repetitive code (template)?

I have a block of code in which there are multiple times the same kind of operations:

void fn() {
  if (params[0].count("VARIABLE_1"))
  {
     STRUCT.VARIABLE_1= boost::lexical_cast<VARIABLE_1_TYPE>(params[0].at("VARIABLE_1"));
  }
  if (params[0].count("VARIABLE_2"))
  {
     STRUCT.VARIABLE_2 = boost::lexical_cast<VARIABLE_2_TYPE>(params[0].at("VARIABLE_2"));
  }
  // many times this kind of if (...) with different parameters
}

Pretty sure there's a more elegant way of writing this in modern C++ (11, 17, 20) using templates I assume. Any idea?

Edit: only the VARIABLE_n and VARIABLE_n_TYPE change, params[0] stays as is.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source