title: Can I use a C++ template library from a C program?
body: <p>My knowledge of C++ is limited. I understand that C++ libraries can be called from C provided a C interface is available. Is it possible to call a C++ template library defined in a <code>.hpp</code> file from a C library? For example, this wonderful <a href="https://fastmathparser.codeplex.com/">expression evaluation</a> library.</p>
  
  <p>It may be time to learn C++!
  OK. Here is a first try:</p>
  
  <pre><code>#ifndef EXPRTK_C_H_
  #define EXPRTK_C_H_
  #include "exprtk.hpp"
  
  //Header
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  typedef exprtk::symbol_table&lt;double&gt;      symbol_table_t;
  typedef exprtk::expression&lt;double&gt;          expression_t;
  typedef exprtk::parser&lt;double&gt;                  parser_t;
  
    void add_variable_(symbol_table_t st, char* name, double *value);
    void register_symbol_table_(expression_t ex, symbol_table_t st);
    void compile_(parser_t parser, char *expression_string, expression_t ex);
    double evaluate_(expression_t ex);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* EXPRTK_C_H_ */
  
  
  #include "exprtk.hpp"
  #include "exprtk_c.hpp"
  </code></pre>
  
  <p>Here is the .cpp file. <code>trig_function()</code> is the real example, used in <code>main()</code>. What follows is my attempt at wrapping the functions. Error message is with parser.</p>
  
  <pre><code>exprtk.hpp:16962:7: error: ‘exprtk::parser&lt;T&gt;::parser(const exprtk::parser&lt;T&gt;&amp;) [with T = double]’ is private
  exprtk_simple_example_01.cpp:85:83: error: within this context
  exprtk_simple_example_01.cpp:60:6: error:   initializing argument 1 of ‘void compile_(parser_t, char*, expression_t)’
  
  template&lt;typename T&gt;
  void trig_function() {
      std::string expression_string =
              "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)";
      T x;
      exprtk::symbol_table&lt;T&gt; symbol_table;
      symbol_table.add_variable("x", x);
      symbol_table.add_constants();
  
      exprtk::expression&lt;T&gt; expression;
      expression.register_symbol_table(symbol_table);
  
      exprtk::parser&lt;T&gt; parser;
      parser.compile(expression_string, expression);
  
      for (x = T(-5.0); x &lt;= T(+5.0); x += 0.001) {
          T y = expression.value();
          printf("%19.15f\t%19.15f\n", x, y);
      }
  }
  
  
  
  //Cpp file
  void add_variable_(symbol_table_t st, char *varname, double *value) {
      const std::string varname_ = varname;
      bool r = st.add_variable(varname_, *value);
      printf("%d\n", r);
      r = st.add_constants();
  }
  
  void register_symbol_table_(expression_t ex, symbol_table_t st) {
      ex.register_symbol_table(st);
  }
  void compile_(parser_t parser,char *expression_string, expression_t ex) {
      const std::string expression_string_ = expression_string;
      parser.compile(expression_string_, ex);
  }
  
  double evaluate_(expression_t ex) {
      double val = ex.value();
      return (val);
  }
  
  int main() {
      trig_function&lt;double&gt;();
  
      double x;
      //exprtk::symbol_table&lt;double&gt; symbol_table;
      symbol_table_t symbol_table;
      add_variable_(symbol_table, "x", &amp;x);
      symbol_table.add_constants();
  
      //exprtk::expression&lt;double&gt; expression;
      expression_t expression;
      register_symbol_table_(expression, symbol_table);
  
      //exprtk::parser&lt;double&gt; parser;
      parser_t parser;
      compile_(parser, "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)", expression);
  
      double q = -5.0, y = 0.0;
      for (x = q; x &lt;= +5.0; x += 0.001) {
          y = evaluate_(expression);
          printf("%19.15f\t%19.15f\n", x, y);
      }
      return (0);
  }
  </code></pre>