chrome中的C++11新特性

本文详细介绍了Chromium项目中采用的C++11新特性,包括auto、decltype、trailing return type等27项,阐述了它们在提升代码效率和可读性上的作用,并提供了相关学习资源。
摘要由CSDN通过智能技术生成

Chromium中的C++11新特性

auto
// src\cc\trees\layer_tree_host_impl.cc
bool LayerTreeHostImpl::IsCurrentlyScrollingViewport() const {
   
  auto* node = CurrentlyScrollingNode();
  if (!node)
    return false;
  if (!viewport()->MainScrollLayer())
    return false;
  return node->id == viewport()->MainScrollLayer()->scroll_tree_index();
}

// src\cc\trees\layer_tree_host_impl_unittest.cc
static void ExpectClearedScrollDeltasRecursive(LayerImpl* root) {
   
  for (auto* layer : *root->layer_tree_impl())
    ASSERT_EQ(ScrollDelta(layer), gfx::Vector2d());
}
decltype
// src\base\bind_internal.h
template <typename T>
decltype(auto) Unwrap(T&& o) {
   
  return Unwrapper<T>::Unwrap(std::forward<T>(o));
}
trailing return type
// src\base\trace_event\memory_usage_estimator.h
template <class T>
auto EstimateMemoryUsage(const T& object)
    -> decltype(object.EstimateMemoryUsage());
default
// src\base\callback.h
template <typename R, typename... Args>
class RepeatingCallback<R(Args...)> : public internal::CallbackBaseCopyable {
   
 public:
  using RunType = R(Args...);
  using PolymorphicInvoke = R (*)(internal::BindStateBase*,
                                  internal::PassingTraitsType<Args>...);

  constexpr RepeatingCallback() = default;

  explicit RepeatingCallback(internal::BindStateBase* bind_state)
      : internal::CallbackBaseCopyable(bind_state) {
   }

  // Copyable and movable.
  RepeatingCallback(const RepeatingCallback&) = default;
  RepeatingCallback& operator=(const RepeatingCallback&) = default;
  RepeatingCallback(RepeatingCallback&&) noexcept = default;
  RepeatingCallback& operator=(RepeatingCallback&&) noexcept = default;

  bool Equals(const RepeatingCallback& other) const {
   
    return EqualsInternal(other);
  }

  R Run(Args... args) const & {
   
    PolymorphicInvoke f =
        reinterpret_cast<PolymorphicInvoke>(this->polymorphic_invoke());
    return f(this->bind_state_.get(), std::forward<Args>(args)...);
  }

  R Run(Args... args) && {
   
    // Move the callback instance into a local variable before the invocation,
    // that ensures the internal state is cleared after the invocation.
    // It's not safe to touch |this| after the invocation, since running the
    // bound function may destroy |this|.
    RepeatingCallback cb = std::move(*this);
    PolymorphicInvoke f =
        reinterpret_cast<PolymorphicInvoke>(cb.polymorphic_invoke());
    return f(cb.bind_state_.get(), std::forward<Args>(args)...);
  }
};
delete
// src\base\callback.h
template <typename R, typename... Args>
class OnceCallback<R(Args...)> : public internal::CallbackBase {
   
 public:
  using RunType = R(Args...);
  using PolymorphicInvoke = R (*)(internal::BindStateBase*,
                                  internal::PassingTraitsType<Args>...);

  constexpr OnceCallback() = default;

  explicit OnceCallback(internal::BindStateBase* bind_state)
      : internal::CallbackBase(bind_state) {
   }

  OnceCallback(const OnceCallback&) = delete;
  OnceCallback& operator=(const OnceCallback&) = delete;

  OnceCallback(OnceCallback&&) noexcept = default;
  OnceCallback& operator=(OnceCallback&&) noexcept = default;

  OnceCallback(RepeatingCallback<RunType> other)
      : internal::CallbackBase(std::move(other)) {
   }

  OnceCallback& operator=(RepeatingCallback<RunType> other) {
   
    static_cast<internal::CallbackBase&>(*this) = std::move(other);
    return *this;
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值