问题描述
我的理解是greater_equal<double>
是一个具有function<bool (double,double)>
签名的函子,因此我尝试执行以下操作,
#include <functional>
#include <memory>
using namespace std;
int main() {
std::shared_ptr<std::function<bool (double,double)>> f;
auto f1 =make_shared<greater_equal<double>>();
auto f2 = make_shared<less_equal<double>>();
f = f1;
}
令我惊讶的是,它不会编译,因为,
'':无法从'const转换 std :: shared_ptrstd :: greater_equal
'到 'std :: shared_ptr >'
我不确定为什么,
我需要的内容与上述代码段中的内容类似,我将拥有一个成员函数std::shared_ptr<std::function<bool (double,double)>>
,并且需要根据某些条件分配<greater_equal<double>
或less_equal<double>
,我应该实现这个目标吗?
解决方法
如果#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_covid19_MainActivity_stringFromJNI(
JNIEnv* env,jobject /* this */) {
std::string hello = "Test string from C to java";
return env->NewStringUTF(hello.c_str());
"()[java/lang/Double;" describes a method expecting no arguments and returning a double array.
jmethodID methodID = env->GetMethodID(MainActivity,"getDouble","()[java/lang/Double;");
}
jobjectarray doubles = env->CallObjectMethod(MainActivity,methodID);
int index = 0;
jdouble doubleArray = env->GetObjectArrayElement(double,index);
和public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
}
public double[] getDouble(){
double[] j = {0,4,5,6,7};
return j;
}
/**
* A native method that is implemented by the 'native-lib' native library,* which is packaged with this application.
*/
public native String stringFromJNI();
}
是从std::greater_equal<double>
继承而来的,那么您写的内容就可以使用。
但是由于std::less_equal<double>
可从std::function<bool(double,double)>
(和std::function<bool(double,double)
)的实例构造而来,因此您可以改为:
std::greater_equal<double>
但是我不认为您首先要less_equal
。
auto f1 = std::make_shared<std::function<bool(double,double)>>(std::greater_equal<double>{});
是具有std::function
签名的函子
是的,它的greater_equal<double>
具有function<bool (double,double)>
参数并返回operator()
,但与(double,double)
无关。
bool
是一个包装器类,可以存储它认为可调用的任何对象。它接受签名的细微变化(例如,即使std::function
通过const引用接受参数,并且您告诉std::function
参数应该按值传递,它仍然有效),并且可以存储带有状态的对象(具有数据成员)。这些功能来自with an overhead。
根据
我需要根据某些条件分配
less/greater_equal
或std::function
您不需要任何greater_equal<double>
功能。
您宁愿使用函数指针(它需要签名以完全匹配,并且不能存储任何状态):
less_equal<double>