I have the following the code:
struct A {//<-- Third party library class
A(){};
A(const A&) = delete;
void init(){};
int i;
};
int f(){
static A a = []{
A res;
res.init();
return res;
}();
return a.i;
}
see it live: http://coliru.stacked-crooked.com/a/a5c5912bd79053c3
And it gives the following error when compiled:
g++ -std=c++17 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp: In lambda function:
main.cpp:12:12: error: use of deleted function 'A::A(const A&)'
return res;
^~~
main.cpp:4:3: note: declared here
A(const A&) = delete;
^
I know I could wrap this in a another struct
and initialize in that constructor, but it just seems somewhat tedious.
Using c++17, do we have a 'neat' way around this 'problem' ?
Any solution must work equally well for a static var in a function