I have a traditional C lib and a function (setsockopts
) wants an argument by pointer. In C++11 (gcc 4.8), can I pass this argument without initializing a named variable?
I have the following, non-satisfying solution:
#include <iostream>
#include <memory>
int deref(int const * p) {return * p;}
using namespace std;
int main() {
int arg = 0; cout << deref(& arg) << endl;
// works, but is ugly (unnecessary identifier)
cout << deref(& 42) << endl;
// error: lvalue required as unary ‘&’ operand
cout << deref(& * unique_ptr<int>(new int(42))) << endl;
// works, but looks ugly and allocates on heap
}