ETLCPP / etl

Embedded Template Library
https://www.etlcpp.com
MIT License
2.24k stars 392 forks source link

Add support for C++11 std::forward #915

Closed tigran2008 closed 4 months ago

tigran2008 commented 4 months ago

EDIT: Looks like it's already in the library, but undocumented!

https://en.cppreference.com/w/cpp/utility/forward

It's very simple to add:

#if ETL_USING_CPP11
  template <class T>
  ETL_CONSTEXPR14 T&& forward(typename etl::remove_reference<T>::type& t) noexcept
  {
    return static_cast<T&&>(t);
  }

  template <class T>
  ETL_CONSTEXPR14 T&& forward(typename etl::remove_reference<T>::type&& t) noexcept
  {
    return static_cast<T&&>(t);
  }
#endif

In order to test:

namespace ForwardTest
{
  constexpr int some_lvalue = 0;
  enum ValueType { LVALUE, RVALUE };

  template <class T>
  ETL_CONSTEXPR14 ValueType get_value_type(T&) { return LVALUE; }

  template <class T>
  ETL_CONSTEXPR14 ValueType get_value_type(T&&) { return RVALUE; }

  template <typename T>
  ETL_CONSTEXPR14 ValueType test(T&& value) {
    return get_value_type(etl::forward<T>(value));
  }
} // namespace ForwardTest

ForwardTest::test(0) would be equal to ForwardTest::RVALUE, whereas ForwardTest(ForwardTest::some_lvalue) would be equal to ForwardTest::LVALUE. To add this test to test/test_utility.cpp:

#if ETL_USING_CPP11
  namespace ForwardTest { /* [...] */ }
#endif
#if ETL_USING_CPP11
  CHECK_EQUAL(ForwardTest::test(0), ForwardTest::RVALUE);
  CHECK_EQUAL(ForwardTest::test(ForwardTest::some_lvalue), ForwardTest::LVALUE);
#endif
tigran2008 commented 4 months ago

P.S.: I felt the need of an etl::forward while implementing the C++20 formatting library using ETL. More specifically, std::basic_format_args

tigran2008 commented 4 months ago

Alright, well, this is awkward. Looks like it was already in the library. I didn't find it in the docs, though. Isn't it supposed to be here? https://www.etlcpp.com/utility.html