r-lib / cpp11

cpp11 helps you to interact with R objects using C++ code.
https://cpp11.r-lib.org/
Other
201 stars 48 forks source link

extra empty names attributes generated by list::push_back #206

Closed mikejiang closed 3 years ago

mikejiang commented 3 years ago
#include "cpp11.hpp"
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace cpp11;
using namespace std;

[[cpp11::register]]
cpp11::writable::list test_cpp() {

  cpp11::writable::list res;
  for (int i = 0; i < 5; i++)
  {
    string chnl = "n" + std::to_string(i);
    res.push_back(cpp11::named_arg(chnl.c_str()) = i);
    auto d = cpp11::as_cpp<vector<string>>(res.names());
    for (auto j : d)
    {
      cout << j << ", ";
    }
    cout << endl;
  }
    return res;

}
> cpp11::cpp_source("~/Downloads/t.cpp")
> d = test_cpp()
n0, 
n0, n1, 
n0, n1, n2, , 
n0, n1, n2, n3, 
n0, n1, n2, n3, n4, , , , 
> names(d)
[1] "n0" "n1" "n2" "n3" "n4" ""   ""   ""  

As shown above, extra names attribute elements are generated and attached to result list. Am I doing something wrong here?