r-lib / cpp11

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

Ambiguous < operation for logical (compiler error) #263

Open vspinu opened 2 years ago

vspinu commented 2 years ago

With the following file

#include <cpp11.hpp>
#include <algorithm>

void c_test(cpp11::logicals t1, cpp11::logicals t2) {
  std::min(t1[0],t2[0]);
}

I get

> cpp11::cpp_source("/home/vspinu/dev/iroll/src/tmp.cpp", quiet = F, clean = F, dir = '/home/vspinu/Dropbox/dev/iroll/')

g++ -std=gnu++11 -I"/home/vspinu/bin/R-4.1.2-bin/include" -DNDEBUG -I'/home/vspinu/.lib/4.1.2/cpp11/include'  -I/usr/local/include   -fpic  -g -O2  -c /home/vspinu/Dropbox/dev/iroll/src/tmp.cpp -o /home/vspinu/Dropbox/dev/iroll/src/tmp.o
In file included from /usr/include/c++/7/algorithm:61:0,
                 from /home/vspinu/.lib/4.1.2/cpp11/include/cpp11/logicals.hpp:3,
                 from /home/vspinu/Dropbox/dev/iroll/src/tmp.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h: In instantiation of ‘const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = cpp11::r_bool]’:
/home/vspinu/Dropbox/dev/iroll/src/tmp.cpp:6:23:   required from here
/usr/include/c++/7/bits/stl_algobase.h:200:15: error: ambiguous overload for ‘operator<’ (operand types are ‘const cpp11::r_bool’ and ‘const cpp11::r_bool’)
       if (__b < __a)
           ~~~~^~~~~
/usr/include/c++/7/bits/stl_algobase.h:200:15: note: candidate: operator<(int, int) <built-in>
/usr/include/c++/7/bits/stl_algobase.h:200:15: note: candidate: operator<(Rboolean, Rboolean) <built-in>
/home/vspinu/bin/R-4.1.2-bin/etc/Makeconf:177: recipe for target '/home/vspinu/Dropbox/dev/iroll/src/tmp.o' failed
make: *** [/home/vspinu/Dropbox/dev/iroll/src/tmp.o] Error 1
In file included from /usr/include/c++/7/algorithm:61:0,
                 from /home/vspinu/.lib/4.1.2/cpp11/include/cpp11/logicals.hpp:3,
                 from /home/vspinu/Dropbox/dev/iroll/src/tmp.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h: In instantiation of ‘const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = cpp11::r_bool]’:
/home/vspinu/Dropbox/dev/iroll/src/tmp.cpp:6:23:   required from here
/usr/include/c++/7/bits/stl_algobase.h:200:15: error: ambiguous overload for ‘operator<’ (operand types are ‘const cpp11::r_bool’ and ‘const cpp11::r_bool’)
       if (__b < __a)
           ~~~~^~~~~
/usr/include/c++/7/bits/stl_algobase.h:200:15: note: candidate: operator<(int, int) <built-in>
/usr/include/c++/7/bits/stl_algobase.h:200:15: note: candidate: operator<(Rboolean, Rboolean) <built-in>
make: *** [/home/vspinu/Dropbox/dev/iroll/src/tmp.o] Error 1
``` R version 4.1.2 (2021-11-01) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 18.04.6 LTS Matrix products: default BLAS: /home/vspinu/bin/R-4.1.2-bin/lib/libRblas.so LAPACK: /home/vspinu/bin/R-4.1.2-bin/lib/libRlapack.so locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=de_DE.UTF-8 LC_MESSAGES=C [7] LC_PAPER=de_DE.UTF-8 LC_NAME=C LC_ADDRESS=C [10] LC_TELEPHONE=C LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] cpp11_0.4.2 loaded via a namespace (and not attached): [1] compiler_4.1.2 ```
vspinu commented 2 years ago

Relatedly automatic promotion to double does not work:


void c_test(cpp11::logicals t1, cpp11::logicals t2) {
  /* std::min(t1[0],t2[0]); */
  double x = t1[0];
}
//    error: conversion from ‘cpp11::r_bool’ to ‘double’ is ambiguous
vspinu commented 2 years ago

Other missing methods on r_bool and double +=, -=, -, *=, *.

Not sure what to do with this. This limitation inhibits development of generic code on doubles, integers and logicals simultaneously.