When I compile and run this with Visual C++ 2010:
#include <iostream>
int main() {
int subtrahend = 5;
struct Subtractor {
int &subtrahend;
int operator()(int minuend) { return minuend - subtrahend; }
} subtractor5 = { subtrahend };
std::cout << subtractor5(47);
}
I get the correct answer, 42.
Nevertheless, the compiler complains that this is impossible:
Temp.cpp(9) : warning C4510:
main::Subtractor: default constructor could not be generated
Temp.cpp(6) : see declaration ofmain::SubtractorTemp.cpp(9) : warning C4512:
main::Subtractor: assignment operator could not be generated
Temp.cpp(6) : see declaration ofmain::SubtractorTemp.cpp(9) : warning C4610:
struct main::Subtractorcan never be instantiated – user defined constructor required
What’s going on?
The first two warnings are just letting you know that the implicitly declared member functions cannot be generated due to the presence of a reference data member.
The third warning is a Visual C++ compiler bug.
All three warnings can be ignored with no ill effects, though you can easily make all three go away by making the reference data member a pointer instead (reference data members are almost never worth the trouble).