u) { return t

问题描述 投票:4回答:1

我见过类似这样的代码。

struct foo_functor {
  template <typename T, typename U>
  constexpr auto operator()(T t, U u) const -> decltype(t | u) {
    return t | u;
  }
};

constexpr foo_functor foo;

据我所知,它和下面的代码一样

template <typename T, typename U>
constexpr auto foo(T t, U u) -> decltype(t | u) {
  return t | u;
}

你为什么要做第一个?有什么不同吗?就我从编译器的输出来看,至少在有了 constexpr没有 那如果他们不是 constexpr在这种情况下,会有什么不同吗?

编辑:作为一个说明,与第一个例子非常相似的代码似乎被用来代替普通函数。6个不同的结构,都只有 operator() 模板,所有的模板都像例子的最后一行一样被实例化。然后每一个都完全像普通函数一样使用。

c++ c++11 c++14
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.