proconlib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: tests/getter_result.test.cpp

Depends on

Code

// competitive-verifier: STANDALONE

#include <iostream>
#include <string>
#include <utility>
#include "tools/assert_that.hpp"
#include "tools/getter_result.hpp"

enum class origin_type {
  default_constructor,
  copy_constructor,
  move_constructor,
  copy_assign,
  move_assign,
};

class inner_type {
  origin_type m_origin;

public:
  constexpr inner_type() : m_origin(origin_type::default_constructor) {
  }
  constexpr inner_type(const inner_type&) : m_origin(origin_type::copy_constructor) {
  }
  constexpr inner_type(inner_type&&) noexcept : m_origin(origin_type::move_constructor) {
  }
  constexpr inner_type& operator=(const inner_type&) {
    this->m_origin = origin_type::copy_assign;
    return *this;
  }
  constexpr inner_type& operator=(inner_type&&) noexcept {
    this->m_origin = origin_type::move_assign;
    return *this;
  }

  constexpr origin_type origin() const {
    return this->m_origin;
  }
};

class outer_type {
  inner_type m_inner;

public:
  constexpr outer_type() = default;

  constexpr auto inner(this auto&& self) -> tools::getter_result_t<decltype(self), inner_type> {
    return std::forward_like<decltype(self)>(self.m_inner);
  }
};

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  outer_type case1{};
  static_assert(std::same_as<decltype(case1.inner()), const inner_type&>);
  assert_that(case1.inner().origin() == origin_type::default_constructor);

  constexpr outer_type case2{};
  static_assert(std::same_as<decltype(case2.inner()), const inner_type&>);
  static_assert(case2.inner().origin() == origin_type::default_constructor);

  static_assert(std::same_as<decltype(outer_type{}.inner()), inner_type>);
  static_assert(outer_type{}.inner().origin() == origin_type::move_constructor);

  static_assert(std::same_as<decltype(static_cast<const outer_type>(outer_type{}).inner()), inner_type>);
  static_assert(static_cast<const outer_type>(outer_type{}).inner().origin() == origin_type::copy_constructor); 

  return 0;
}
#line 1 "tests/getter_result.test.cpp"
// competitive-verifier: STANDALONE

#include <iostream>
#include <string>
#include <utility>
#line 1 "tools/assert_that.hpp"



#line 5 "tools/assert_that.hpp"
#include <cstdlib>

#define assert_that_impl(cond, file, line, func) do {\
  if (!cond) {\
    std::cerr << file << ':' << line << ": " << func << ": Assertion `" << #cond << "' failed." << '\n';\
    std::exit(EXIT_FAILURE);\
  }\
} while (false)
#define assert_that(...) assert_that_impl((__VA_ARGS__), __FILE__, __LINE__, __func__)


#line 1 "tools/getter_result.hpp"



#include <type_traits>

namespace tools {
  template <typename T, typename U>
  struct getter_result {
    using type = std::conditional_t<std::is_lvalue_reference_v<T>, const U&, U>;
  };

  template <typename T, typename U>
  using getter_result_t = typename tools::getter_result<T, U>::type;
}


#line 8 "tests/getter_result.test.cpp"

enum class origin_type {
  default_constructor,
  copy_constructor,
  move_constructor,
  copy_assign,
  move_assign,
};

class inner_type {
  origin_type m_origin;

public:
  constexpr inner_type() : m_origin(origin_type::default_constructor) {
  }
  constexpr inner_type(const inner_type&) : m_origin(origin_type::copy_constructor) {
  }
  constexpr inner_type(inner_type&&) noexcept : m_origin(origin_type::move_constructor) {
  }
  constexpr inner_type& operator=(const inner_type&) {
    this->m_origin = origin_type::copy_assign;
    return *this;
  }
  constexpr inner_type& operator=(inner_type&&) noexcept {
    this->m_origin = origin_type::move_assign;
    return *this;
  }

  constexpr origin_type origin() const {
    return this->m_origin;
  }
};

class outer_type {
  inner_type m_inner;

public:
  constexpr outer_type() = default;

  constexpr auto inner(this auto&& self) -> tools::getter_result_t<decltype(self), inner_type> {
    return std::forward_like<decltype(self)>(self.m_inner);
  }
};

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  outer_type case1{};
  static_assert(std::same_as<decltype(case1.inner()), const inner_type&>);
  assert_that(case1.inner().origin() == origin_type::default_constructor);

  constexpr outer_type case2{};
  static_assert(std::same_as<decltype(case2.inner()), const inner_type&>);
  static_assert(case2.inner().origin() == origin_type::default_constructor);

  static_assert(std::same_as<decltype(outer_type{}.inner()), inner_type>);
  static_assert(outer_type{}.inner().origin() == origin_type::move_constructor);

  static_assert(std::same_as<decltype(static_cast<const outer_type>(outer_type{}).inner()), inner_type>);
  static_assert(static_cast<const outer_type>(outer_type{}).inner().origin() == origin_type::copy_constructor); 

  return 0;
}
Back to top page