Skip to content
Snippets Groups Projects
  1. Oct 27, 2017
    • Jö Fahlke's avatar
      [autoCopy()] Unproxying and evaluation helper for `auto`. · 62ec1946
      Jö Fahlke authored
      This introduces a helper function that enables `auto` type deduction even in
      the presence of proxies and pre-C++11 libraries with a tendency to capture
      references to temporaries.
      
      Note: the motivating examples below make use of specializations of
            `autoCopy()` that will be introduced in later commits.
      
      Example 1: deducing the element type of a vector when proxies are a
      possibility:
      ```c++
      template<class Vector>
      auto pop_and_clear(Vector &v)
      {
        auto head = autoCopy(v[0]);
        v.clear();
        return head;
      }
      ```
      Without `autoCopy()` this would be undefined behaviour if `Vector` happens to
      be `std::vector<bool>`.
      
      Example 2: a custom number type with lazy evaluation:
      ```c++
      template<class MaybeCustomType>
      auto f(MaybeCustomType v)
      {
        return 2 * v - 1;
      }
      ```
      This fails for the custom number type from the automatic differentiation
      library Adept, because Adept constructs an expression object that holds
      references to its subexpressions and is evaluated only when it is assigned to
      a number object later.  Due to the use of `auto` this expression object is
      passed out of the function, but the temporary object for the subexpression
      `2*v` is destroyed when the function returns.  Wrapping the expression in the
      `return` statement with `autoCopy()` ensures that the evaluation happens while
      the reference to the subexpression is still valid, and has no ill effect when
      fundamental types instead of Adept's custom types are used.
      62ec1946
  2. Oct 26, 2017
  3. Oct 24, 2017
  4. Oct 23, 2017
  5. Oct 16, 2017
  6. Oct 11, 2017
  7. Oct 10, 2017
  8. Oct 06, 2017
  9. Oct 05, 2017
  10. Oct 04, 2017
  11. Oct 02, 2017
  12. Sep 29, 2017
  13. Sep 26, 2017
  14. Sep 23, 2017
Loading