C++ 为什么std::forward放弃constexpr ness?
由于未声明C++ 为什么std::forward放弃constexpr ness?,c++,c++11,constants,move-semantics,perfect-forwarding,C++,C++11,Constants,Move Semantics,Perfect Forwarding,由于未声明constexpr,std::forward将放弃它将参数转发到的任何函数的constexpr ness为什么std::forward没有声明constexpr本身,这样它就可以保存constexpr属性? 示例:(使用g++snapshot-2011-02-19进行测试) 我的问题是:std::forward为什么不像fix::forward那样定义 注2:这个问题与我的另一个问题有些关联,因为std::forward不是constexpr是无法通过使用右值调用其cstr来创建std
constexpr
,std::forward
将放弃它将参数转发到的任何函数的constexpr ness为什么std::forward
没有声明constexpr
本身,这样它就可以保存constexpr属性?
示例:(使用g++snapshot-2011-02-19进行测试)
我的问题是:std::forward
为什么不像fix::forward
那样定义
注2:这个问题与我的另一个问题有些关联,因为
std::forward
不是constexpr
是无法通过使用右值调用其cstr来创建std::tuple
的技术原因,但这里的这个问题显然是(很多)一般来说, < P>一般的答案是,C++委员会的图书馆工作组没有通过工作草案进行详尽的调查,寻找新的核心设施的使用机会。当人们有时间和倾向于研究可能的用途时,这些特性就被使用了,但是没有时间进行详尽的检查
有一些论文是关于
constepr
在作品中的其他用法的,例如。谢谢。我已经知道相关文件,但它没有提到关于std::forward的第20.3.3节。-你提供的更多的是一个“社会原因”。我对一个“概念性原因”感兴趣,即,制作std::forward constexpr是否有任何错误,或者可以吗?我看不出有任何原因不能乍一看。关于如何在constexpr函数中传递和调用函数指针的问题报告(文字函数对象类型可能已经是constexpr),让std::forward成为非constexpr似乎很不幸。建议为std::forward
快速注释添加constepr,以\uuz]
开头的标识符保留给编译器实现者。你的程序本身就是一个不规范的程序。如果你移动到Windows,它是一个宏…@Matthieu M,@Bo Persson,谢谢。我用T等替换了所有的名字,这样其他人可以更安全/容易地尝试代码。
#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);
}
#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);
}