My C++ compiler is C++ 14. I have a function which intakes an object of a custom class which encapsulates all kinds of error codes. This parameter is passed by reference. The custom class object sometimes is populated with a valid error value & at times it can be empty. I found a nice looking new thing in C++14 that is std::experimental::optional. Now I am trying to use this for my parameter as it is really an optional parameter.
Following is my function's signature with std::experimental::optional that I am trying to use:
MyFunction(some param, std::experimental::optional<MyCustomErrorClass> & error_code) {
//some logic
//sometimes error_code is populated & sometimes not populated
}
Following is how I am calling MyFunction:
MyCustomErrorClass error_code_object;
MyFunction(some_param, error_code_object);
But I receive the following compiler error:
error: non-const lvalue reference to type 'std::experimental::optional< MyCustomErrorClass>' cannot bind to a value of unrelated type 'MyCustomErrorClass' MyFunction(some_param, error_code_object);
I tried searching this a lot. Most of the std::experimental::optional or std::optional usage examples demonstrate it as a return value of functions.
What is wrong with my usage of std::experimental::optional ?
error_codepassed by non-const reference? If it's purely an input parameter, pass it by const-ref just as you would with any other parameter... - ildjarnMyFunctionwhen error occurs - TheWaterProgrammeroptional. So how is theoptionalout param supposed to be accessed in the calling scope? - Lightness Races in Orbit