title: Return subset of a given SEXP without knowing the actual internal data type
body: <p>I am writing an <a href="https://github.com/aryoda/R_CppDebugHelper" rel="nofollow noreferrer">R package to provide debug helper functions</a> for <code>gdb</code> to print variable values of <code>R</code>s <code>SEXP</code> and <code>Rcpp</code>s data types.</p>
  
  <p>C/C++ is a strongly typed language but I want to avoid querying the internal data type of the <code>SEXP</code> and use a bunch of <code>if</code>s for hard-coded dispatching.</p>
  
  <p><strong>How could I implement a generic subset function using an index range via a function signature like this:</strong></p>
  
  <pre><code>SEXP dbg_subset(SEXP x, R_xlen_t index_from, R_xlen_t index_to);
  </code></pre>
  
  <p>One possible solution would be to write the above function for each possible signature, eg.
  for <code>INTSXP</code>, <code>LGLSXP</code>, <code>STRSXP</code>... but I am lazy ;-)</p>
  
  <p>Note: I can't use C++ templates because the linker would only create the template instances for the data types used in my C++ code (which are not the same as in the "library under debugging") and <code>gdb</code>
  is no compiler which could create the missing template instances "on-the-fly" when C++ expressions are entered to query variables.</p>
  
  <p><strong>Edit:</strong> See <a href="https://stackoverflow.com/a/19829440">this answer</a> (but it is based on templates which I want to avoid):</p>