Uploaded image for project: 'C++ Driver'
  1. C++ Driver
  2. CXX-963

How can I return a bsoncxx::document::element from a function?

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Trivial - P5 Trivial - P5
    • None
    • Affects Version/s: 3.0.1
    • Component/s: Implementation
    • Labels:
      None
    • Environment:
      Windows 10
      MS VS 2015
      MongoDB 3.3.5
      MongoDB C++11 Driver 3.0.1

      #include <bsoncxx/builder/stream/document.hpp>
      #include <iostream>
      
      bsoncxx::document::element return_element()
      {
      	auto build_doc = bsoncxx::builder::stream::document{};
      	build_doc << "key_field" << "hello world";
      	bsoncxx::document::element element_to_return = build_doc.view()["key_field"];
      	// outputs: key_field hello world
      	std::cout << element_to_return.key().to_string() << " " << element_to_return.get_utf8().value;
      	return element_to_return;
      }
      
      int main()
      {
      	bsoncxx::document::element returned_element = return_element();
      	// the next line causes a debug error at element.cpp
      	// types::b_utf8 element::get_utf8() const {
      	//	 BSONCXX_TYPE_CHECK(k_utf8);
      	std::cout << returned_element.get_utf8().value;
          return 0;
      }
      

      The returned_element is "junk". Is there a way to preserve an element from a function?

            Assignee:
            Unassigned Unassigned
            Reporter:
            therefore George Thompson
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: