JNIENV * capture inside C ++ lambda expression error

In my Main.cpp file, I have a JNI call:

JNIEXPORT jstring   JNICALL
    Java_packagename_MainActivity_GenerateAlphabet(JNIEnv *env, jobject thiz)
        {
            generateAlphabet([](string letters, string numbers) {
                        return (env)->NewStringUTF("test");

            });
        }

but this code gives me an error:

jni / Main.cpp: lambda function:

jni / Main.cpp: 86: 33: error: 'env' not writing

I really need to use env in lambda function, can anyone help me.

EDIT:

The accepted answer solves the problem, but keep in mind that you cannot return the value inside the lambda expression.

+4
source share
2 answers

You can put envon a capture list with [env].

, . env . - , .

JavaVM* ( env), AttachCurrentThread, env .

+4

++ , lambda exprsesion (aka capture).

[] - . env , env ( ):

[env](string letters, string numbers) {
    return (env)->NewStringUTF("test");
});
0

Source: https://habr.com/ru/post/1599519/


All Articles