Re: [boost] [optional] generates unnessesary code for trivial types

I use optional references in my code, in cases like the following:
I have an operation, which processes some elements, and some of the elements constitute special cases. The caller of the operation may or may not want to know about the special cases that arose, so they can optionally pass in a container which will be populated with the special cases:
void some_operation(inputs, optional<vector<case>&> special_cases = none) { for (...) { ... if (special_case) { ... if (special_cases) special_cases->push_back(current_case); } } }
Before I discovered optional, I used a plain reference, but that was annoying because I had to create a dummy vector to be used as the default argument. (The other alternative would have been to use a pointer, but then the caller has to use the uglier syntax of passing in "&special_cases" rather than "special_cases").
Nathan, going back to the same question I asked Andrey ( http://groups.google.com/group/boost-developers-archive/msg/704971a1eb63b3d2), it looks like your use case would still work if we disabled any sort of assignment for optional references (even the assignment from boost::none for resetting). Am I correct? Regards, &rzej

From: akrzemi1@gmail.com
I use optional references in my code, in cases like the following:
I have an operation, which processes some elements, and some of the elements constitute special cases. The caller of the operation may or may not want to know about the special cases that arose, so they can optionally pass in a container which will be populated with the special cases:
void some_operation(inputs, optional<vector<case>&> special_cases = none) { for (...) { ... if (special_case) { ... if (special_cases) special_cases->push_back(current_case); } } }
Before I discovered optional, I used a plain reference, but that was annoying because I had to create a dummy vector to be used as the default argument. (The other alternative would have been to use a pointer, but then the caller has to use the uglier syntax of passing in "&special_cases" rather than "special_cases").
Nathan, going back to the same question I asked Andrey ( http://groups.google.com/group/boost-developers-archive/msg/704971a1eb63b3d2), it looks like your use case would still work if we disabled any sort of assignment for optional references (even the assignment from boost::none for resetting). Am I correct?
You are correct. I was providing a use case for optional references in general, not one for assignment to optional references. (That's not to say I think use cases for assignment to optional references don't exist - I justĀ don't have one in mind right now). Regards, Nate
participants (2)
-
Andrzej Krzemienski
-
Nathan Ridge