无法使用c++11编译简单的Rcpp函数

Unable to compile simple Rcpp function using c++11

问题背景

升级到 后,我似乎无法编译使用 // [[Rcpp::plugins(cpp11)]] 的简单 Rcpp 函数。我试过:

代码

函数,我起草了生成错误:

#include <Rcpp.h>
#include <string.h> // std::string, std::stod
using namespace Rcpp;

// [[Rcpp::plugins(cpp11)]]

// [[Rcpp::export]]
NumericVector convertToDouble(Rcpp::StringVector x) {
    // Numeirc vector to store results
    NumericVector res;
    // Double for converted values
    double converted_double;
    for(Rcpp::StringVector::iterator it = x.begin(); it != x.end(); ++it) {
        // Get [] for vector element
        int index = std::distance(x.begin(), it);
        // Add results to vector
        converted_double = std::stod(x[index]);
        res.push_back(converted_double);
    }
    return res;
}


// Source / test

/*** R
Rcpp::sourceCpp(
    file = "sample_stod.cpp",
    embeddedR = FALSE,
    verbose = TRUE,
    rebuild = TRUE,
    cleanupCacheDir = TRUE,
    showOutput = TRUE)
convertToDouble(c("2.3", "34.25a", "abc32def.43", "12", "1.5"))
*/

错误

>> Rcpp::sourceCpp(
+     file = "sample_stod.cpp",
+     embeddedR = FALSE,
+     verbose = TRUE,
+     rebuild = TRUE,
+     cleanupCacheDir = TRUE,
+     showOutput = TRUE)

Generated extern "C" functions 
--------------------------------------------------------


#include <Rcpp.h>
// convertToDouble
NumericVector convertToDouble(Rcpp::StringVector x);
RcppExport SEXP sourceCpp_1_convertToDouble(SEXP xSEXP) {
BEGIN_RCPP
    Rcpp::RObject rcpp_result_gen;
    Rcpp::RNGScope rcpp_rngScope_gen;
    Rcpp::traits::input_parameter< Rcpp::StringVector >::type x(xSEXP);
    rcpp_result_gen = Rcpp::wrap(convertToDouble(x));
    return rcpp_result_gen;
END_RCPP
}

Generated R functions 
-------------------------------------------------------

`.sourceCpp_1_DLLInfo` <- dyn.load('/private/var/folders/7x/kwc1y_l96t55_rwlv35mg8xh0000gn/T/Rtmp2H7VYU/sourceCpp-x86_64-apple-darwin15.6.0-0.12.19/sourcecpp_295d2f3a47a3/sourceCpp_2.so')

convertToDouble <- Rcpp:::sourceCppFunction(function(x) {}, FALSE, `.sourceCpp_1_DLLInfo`, 'sourceCpp_1_convertToDouble')

rm(`.sourceCpp_1_DLLInfo`)

Building shared library
--------------------------------------------------------

DIR: /private/var/folders/7x/kwc1y_l96t55_rwlv35mg8xh0000gn/T/Rtmp2H7VYU/sourceCpp-x86_64-apple-darwin15.6.0-0.12.19/sourcecpp_295d2f3a47a3

/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB -o 'sourceCpp_2.so' --preclean  'sample_stod.cpp'  
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I"/Users/huski/Documents/R Projects/RcppConversion" -I/usr/local/include   -fPIC  -Wall -g -O2 -c sample_stod.cpp -o sample_stod.o
sample_stod.cpp:17:28: error: no matching function for call to 'stod'
        converted_double = std::stod(x[index]);
                           ^~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3910:30: note: candidate function not viable: no known conversion from 'Rcpp::Vector<16, PreserveStorage>::Proxy' (aka 'string_proxy<16, PreserveStorage>') to 'const std::__1::string' (aka 'const basic_string<char, char_traits<char>, allocator<char> >') for 1st argument
_LIBCPP_FUNC_VIS double      stod (const string& __str, size_t* __idx = 0);
                             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3930:30: note: candidate function not viable: no known conversion from 'Rcpp::Vector<16, PreserveStorage>::Proxy' (aka 'string_proxy<16, PreserveStorage>') to 'const std::__1::wstring' (aka 'const basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >') for 1st argument
_LIBCPP_FUNC_VIS double      stod (const wstring& __str, size_t* __idx = 0);
                             ^
1 error generated.
make: *** [sample_stod.o] Error 1
Error in Rcpp::sourceCpp(file = "sample_stod.cpp", embeddedR = FALSE,  : 
  Error 1 occurred building shared library.
>> 

~/.R/Makevars

# Force use of compilers maintained by Homebrew

# Clang and clang++
CC=/usr/local/opt/llvm/bin/clang 
CXX=/usr/local/opt/llvm/bin/clang++
CXX1X=clang-omp++

尝试传递 CC \ CXX \ CXX1X 变量以使用通过 Homebrew 安装的编译器,但确实会生成不同的错误消息。所有编译尝试都在

上失败

sample_stod.cpp:17:28: error: no matching function for call to 'stod'


因此,除了与 macOS 上的编译器恶作剧有关的最初问题(已涵盖 here)之外,在从中的单个值转换为 std::string 时,您必须帮助编译器Rcpp::StringVector 在这种情况下。

#include <Rcpp.h>

// [[Rcpp::plugins(cpp11)]]

// [[Rcpp::export]]
Rcpp::NumericVector convertToDouble(Rcpp::StringVector x) {
    // Numeirc vector to store results
    std::vector<double> res;

    // Double for converted values
    double converted_double;
    for(Rcpp::StringVector::iterator it = x.begin(); it != x.end(); ++it) {
        // Get [] for vector element
        int index = std::distance(x.begin(), it);

        // Help the conversion to string
        std::string temp = Rcpp::as<std::string>(x[index]);

        // Convert
        converted_double = std::stod(temp);

        // Add to a std vector... Do not use with Rcpp types
        res.push_back(converted_double);
    }

    // Convert and return the Rcpp type as desired.
    return Rcpp::wrap(res);
}

那么,我们可以运行:

convertToDouble(c("2.3", "34.25a", "abc32def.43", "12", "1.5"))
# Error in convertToDouble(c("2.3", "34.25a", "abc32def.43", "12", "1.5")) : stod: no conversion

此错误位于:abc32def.43

您可能需要在尝试转换之前对字符串进行额外的清理...