[CXX-963] How can I return a bsoncxx::document::element from a function? Created: 03/Jul/16 Updated: 08/Jan/24 Resolved: 13/Jul/16 |
|
| Status: | Closed |
| Project: | C++ Driver |
| Component/s: | Implementation |
| Affects Version/s: | 3.0.1 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Trivial - P5 |
| Reporter: | George Thompson | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Windows 10 |
||
| Description |
The returned_element is "junk". Is there a way to preserve an element from a function? |
| Comments |
| Comment by Mira Carey [ 13/Jul/16 ] | |||||||||||||||||||||||
|
It seems like you've found a work around for your issue in types::value (which is what that type was designed for). If have more problems, feel free to open new issues. | |||||||||||||||||||||||
| Comment by Andrew Morrow (Inactive) [ 11/Jul/16 ] | |||||||||||||||||||||||
|
therefore - That looks good, however I would like to call out one subtlety. The b_utf8 object that you are returning is a 'view' into a string. In this case, it works, because the string "string" is a string literal, and therefore outlives the function. Had you passed in std::string("string") however to the b_utf8 constructor it would have been a use-after-free. Just something to keep in mind. Overall, there is no concept of an owned element in the driver. I think it might be possible to build such an abstraction, however. I'm envisioning a class that wraps a std::pair<std::shared_ptr<document::value>, bsoncxx::types::value>>, though I haven't completely thought it through. | |||||||||||||||||||||||
| Comment by George Thompson [ 04/Jul/16 ] | |||||||||||||||||||||||
|
I ended up using bsoncxx::types::value to do what I want:
| |||||||||||||||||||||||
| Comment by George Thompson [ 03/Jul/16 ] | |||||||||||||||||||||||
|
I get that the element has a pointer to the document view that is no longer in scope. Is there a way, nonetheless, to return an element that I can then use to build a document in main? The objective is the value of the element could be one of several types. I previously used a BSONObj to achieve this. |