我创建了一个std::forward别名,其行为应与std::forward.
template<class T>
constexpr decltype(auto) fwd(T mValue) noexcept
{
return std::forward<T>(mValue);
}
然后我在我的代码库中替换了所有出现的std::forward<...>with fwd<...>。
编译了所有项目g++ 4.9- 所有测试都通过了,一切正常。
然后我尝试用clang++ 3.5. 一些测试似乎随机失败,原因是fwd<...>. 将其替换为std::forward<...>再次修复了失败的测试。
我尝试fwd<...>使用尾随返回类型语法编写,因为我认为decltype(auto)它不起作用:
template<class T>
constexpr auto fwd(T mValue) noexcept -> decltype(std::forward<T>(mValue))
{
return std::forward<T>(mValue);
}
相同的结果:g++有效,clang++无效。
然后我查找了std::forwardcppreference 的签名,并像这样实现了我的别名:
template<class T>
constexpr T&& fwd(std::remove_reference_t<T>& t) { return std::forward<T>(t); }
template<class T>
constexpr T&& fwd(std::remove_reference_t<T>&& t) { return std::forward<T>(t); }
g++这在和上都有效(所有测试都通过)clang++。
为什么decltype(auto)版本不工作?它不应该返回与 完全相同的返回类型std::forward吗?