Statische lambda initialisieren ?
-
Hallo,
in dieser Mustache Library wird gesagt, dass es möglich ist eine Escape Funktion zu überschreiben.
https://github.com/no1msd/mstch/commit/57196d08c52bc73bd923b830a2e59f86d55d8d7c
Das funktioniert wenn die Lambda Funktion innerhalb der main angelegt wird, aber ich frage mich warum/was hier die Erwartung vom Compiler ist:
#include <iostream> #include <algorithm> #include <functional> using namespace std; struct a { static std::function<int(const int )> my_lambda; } l; // geht gar nicht // a::my_lambda = [](const int in) { return 4 * in; } ; int main() { // geht so lala a::my_lambda = [](const int in) { return 4 * in; } ; int lambda_out = a::my_lambda(3); cout << lambda_out << endl; return 0; }
-
Balabala?
Fehlermeldung!
-
/usr/bin/ld: /home/v6nX3s/prog-659e02.o: in function `main': prog.cpp:(.text+0xa): undefined reference to `a::my_lambda' /usr/bin/ld: prog.cpp:(.text+0x16): undefined reference to `a::my_lambda' /usr/bin/ld: prog.cpp:(.text+0x3c): undefined reference to `a::my_lambda' /usr/bin/ld: prog.cpp:(.text+0x5a): undefined reference to `a::my_lambda' /usr/bin/ld: prog.cpp:(.text+0x78): undefined reference to `a::my_lambda' /usr/bin/ld: /home/v6nX3s/prog-659e02.o:prog.cpp:(.text+0x7e): more undefined references to `a::my_lambda' follow
erwartung waere dass die funktion zumindest da ist (wobei der scope bei der initialisierung wohl eine rolle spielt, weshalb geht es in main, und nicht im global scope?)
-
So funktioniert es:
#include <iostream> #include <algorithm> #include <functional> using namespace std; struct a { static std::function<int(const int )> my_lambda; } sa; // geht jetzt da nun auch der typ vorhanden ist um die statische variable zu definieren std::function<int(const int )> a::my_lambda = [](const int in) { return 4 * in; } ; int main() { // geht so lala //a::my_lambda = [](const int in) { return 4 * in; } ; int lambda_out = a::my_lambda(3); cout << lambda_out << endl; return 0; }
Edit: mit dem original code liefert der gcc 8.3.0 folgende fehlermeldung:
mytest.cpp:13:4: error: ‘my_lambda’ in ‘struct a’ does not name a type a::my_lambda = [](const int in) { return 4 * in; } ; ^~~~~~~~~ mytest.cpp:8:43: note: ‘a::my_lambda’ declared here static std::function<int(const int )> my_lambda;
-
#include <iostream> #include <algorithm> #include <functional> using namespace std; struct a { static std::function<int(const int )> my_lambda; } sa; int main() { // geht so lala a::my_lambda = [](const int in) { return 4 * in; } ; int lambda_out = a::my_lambda(3); cout << lambda_out << endl; return 0; }
Das liefert mit gcc 8.3.0
/usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: /tmp/ccgmHcgZ.o: warning: relocation against `_ZN1a9my_lambdaE' in read-only section `.text' /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: /tmp/ccgmHcgZ.o: in function `main': mytest.cpp:(.text+0x34): undefined reference to `a::my_lambda' /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: mytest.cpp:(.text+0x45): undefined reference to `a::my_lambda' /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: warning: creating a DT_TEXTREL in object collect2: error: ld returned 1 exit status
Was auch klar ist da im obigen code a::my_lambda nirgends definiert wird es gibt nur eine deklaration
-
beachte auch im github link die Änderung in der datei "src/mstch.cpp"
denn dort wird die escape variable definiert.
-
weshalb geht es in main,
???
Dein Test zeigt, dass es in main nicht geht.
Mit int hättest du das gleiche Problem.