简体   繁体   English

为什么std :: forward丢弃constexpr-ness?

[英]Why does std::forward discard constexpr-ness?

Being not declared constexpr , std::forward will discard constexpr-ness for any function it forwards arguments to. 由于没有声明constexprstd::forward将丢弃constexpr-ness用于转发参数的任何函数。 Why is std::forward not declared constexpr itself so it can preserve constexpr-ness? 为什么std::forward没有声明constexpr本身,所以它可以保留constexpr-ness?

Example: (tested with g++ snapshot-2011-02-19) 示例:(使用g ++ snapshot-2011-02-19测试)

#include <utility>

template <typename T> constexpr int f(T x) { return -13;}
template <typename T> constexpr int g(T&& x) { return f(std::forward<T>(x));}

int main() {
  constexpr int j = f(3.5f);
  // next line does not compile: 
  // error: ‘constexpr int g(T&&) [with T = float]’ is not a constexpr function
  constexpr int j2 = g(3.5f);
}

Note: technically, it would be easy to make std::forward constexpr, eg, like so (note that in g std::forward has been replaced by fix::forward ): 注意:从技术上讲,很容易制作std::forward constexpr,例如,如此(请注意,在g std::forward中已被fix::forward替换):

#include <utility>

namespace fix {
  /// constexpr variant of forward, adapted from <utility>:
  template<typename Tp>
  inline constexpr Tp&&
  forward(typename std::remove_reference<Tp>::type& t) 
  { return static_cast<Tp&&>(t); }

  template<typename Tp>
  inline constexpr Tp&&
  forward(typename std::remove_reference<Tp>::type&& t) 
  {
    static_assert(!std::is_lvalue_reference<Tp>::value, "template argument"
          " substituting Tp is an lvalue reference type");
    return static_cast<Tp&&>(t);
  }
} // namespace fix

template <typename T> constexpr int f(T x) { return -13;}
template <typename T> constexpr int g(T&& x) { return f(fix::forward<T>(x));}

int main() {
  constexpr int j = f(3.5f);
  // now compiles fine:
  constexpr int j2 = g(3.5f);
}

My question is: why is std::forward not defined like fix::forward ? 我的问题是:为什么std::forward没有定义像fix::forward

Note2: this question is somewhat related to my other question about constexpr std::tuple as std::forward not being constexpr is the technical reason why std::tuple cannot be created by calling its cstr with rvalues, but this question here obviously is (much) more general. 注2:这个问题与我关于constexpr std :: tuple的其他问题有些相关,因为std::forward不是constexpr是为什么std::tuple不能通过用rvalues调用它的cstr来创建的技术原因,但这里的问题显然是(更多)更一般。

The general answer is that the C++ committee's Library Working Group have not done an exhaustive trawl through the working draft looking for opportunities to use the new core facilities. 一般的答案是,C ++委员会的图书馆工作组尚未通过工作草案进行详尽的搜索,寻找使用新核心设施的机会。 These features have been used where people have had the time and inclination to look at possible uses, but there is not the time for exhaustive checking. 这些功能已被用于人们有时间和倾向于查看可能用途的地方,但是没有时间进行详尽的检查。

There are some papers regarding additional uses of constexpr in the works, such as those in the November 2010 mailing . 有一些关于constexpr在工作中的其他用途的论文,例如2010年11月邮件中的那些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM