【问题标题】:Would you ever mark a C++ RValue reference parameter as const您是否曾经将 C++ RValue 引用参数标记为 const
【发布时间】:2013-01-22 10:03:41
【问题描述】:

我一直在切换模板工厂函数以使用(并理解)std::forward 来支持右值和移动语义。我通常用于模板类的样板工厂函数总是将参数标记为 const:

#include <iostream>
#include <utility>

template<typename T, typename U>
struct MyPair{
    MyPair(const T& t, const U& u):t(t),u(u){};

    T t;
    U u;
};

template<typename T, typename U>
std::ostream& operator<<(std::ostream& os, const MyPair<T,U>& pair){
    os << "(" << pair.t << ")=>" << pair.u;
    return os;
}

template<typename T, typename U>
MyPair<T,U> MakeMyPair(const T& t, const U& u){
    return MyPair<T,U>(t,u);
}

using namespace std;
int main(int argc, char *argv[]) {    

    auto no_forward = MakeMyPair(num, num);
    std::cout << no_forward << std::endl;

    auto no_forward2 = MakeMyPair(100, false);
    std::cout << no_forward2 << std::endl;
}

按预期编译。最初我将 MakeMyPair 转换为也将参数作为 const 传递,但这不会在我的 Mac 上使用 XCode 4.6 编译:

//$ clang --version
//Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
//Target: x86_64-apple-darwin12.2.0
//Thread model: posix


template<typename T, typename U>
MyPair<T,U> MakeMyPair_Forward(const T&& t, const U&& u){
    return MyPair<T,U>(std::forward<const T>(t),std::forward<const U>(u));
}

int main(int argc, char *argv[]) { 
    int num = 37;
    auto anotherPair = MakeMyPair_Forward(num, true); //This won't work

    auto allRvalues = MakeMyPair_Forward(73, false);   //will compile 
    std::cout << allRvalues  << std::endl;
}

没有匹配函数调用“MakeMyPair_Forward”候选人 函数 [with T = int, U = bool] 不可行:没有已知的转换 第一个参数的“int”到“const int &&”

这从http://en.cppreference.com/w/cpp/utility/forward 中是有道理的,其中状态 const 是推导出来的,我正在传递左值。

  • 如果对 wrapper() 的调用传递了一个右值 std::string,则 T 被推导出为 std::string(不是 std::string&、const std::string& 或 std::string&&) 和 std::forward 确保右值引用是 传递给 foo。
  • 如果对 wrapper() 的调用传递一个 const 左值 std::string,则 T 被推导出为 const std::string&,而 std::forward 确保一个 const 左值引用被传递给 foo。
  • 如果对 wrapper() 的调用传递了非常量左值 std::string,则 T 被推导出为 std::string&,而 std::forward 确保非常量 左值引用被传递给 foo。

删除 const 对右值和左值起作用。只有将右值作为类型传递才能在 MakeMyPair_Forward 的参数上使用 const。

//This works for rvalues and lvalues
template<typename T, typename U>
MyPair<T,U> MakeMyPair_Forward(T&& t, U&& u){
    return MyPair<T,U>(std::forward<const T>(t),std::forward<const U>(u));
}

所以,问题。作为参数传递时将右值引用标记为 const 是否有意义?这不像我可以更改右值,这只是暂时的。在完成和修复我的代码之后,我对它使用 const 编译感到有点惊讶。为什么要将右值参数标记为 const?重点是只提供一个接受右值的 API 吗?如果是这样,您不会使用类型特征来防止左值引用吗? https://stackoverflow.com/a/7863645/620304

谢谢。

【问题讨论】:

  • 如果你不想修改它,它是否是临时的并不重要。
  • 我的想法是你不能修改它,因为它是临时的。所以标记为 const 对我来说真的没有意义。
  • 为什么不能修改临时文件?
  • 我明白你的意思。你说得对。你可以做类似 int&& i = 3;我++;我更多地考虑将 3 作为右值传递。我只是习惯于在我的工厂中强制使用 const,因为我习惯于只处理左值。我想这是我个人 C++11 学习曲线的一部分。
  • 我不会因为重复而投票关闭,但至少链接应该放在这里:stackoverflow.com/questions/4938875/…

标签: c++ c++11 constants rvalue-reference perfect-forwarding


【解决方案1】:

所以,问题。标记右值引用是否有意义 作为参数传递时作为const?

这是在 C++11 标准中完成的一个地方:

template <class T> reference_wrapper<T> ref(T&) noexcept;
template <class T> reference_wrapper<const T> cref(const T&) noexcept;
template <class T> void ref(const T&&) = delete;
template <class T> void cref(const T&&) = delete;

const T&amp;&amp; 用于捕获所有右值,无论是否为 const,并将它们抛给编译时错误,同时允许左值,甚至 const 左值绑定和工作。

现在这也可以通过T&amp;&amp;enable_if 约束来完成。但是,如果 C++ 在过去的几十年里教会了我们一件事:不要在语言设计中烧毁任何桥梁。 C++ 程序员经常会找到一种巧妙的方法来使用最初被认为无用的语言特性。正是本着这种精神,const T&amp;&amp; 被保留为合法选项。

【讨论】:

  • 这似乎是一个有效的用例。它也更容易阅读,并且使用类型特征可能会使其更难实现。我没有想过使用 delete 关键字删除函数和隐式创建的构造函数。
  • 仅供参考,这些不是构造函数,而是删除了命名空间范围的函数。而 that 是 C++11 中一个非常酷的新事物,一种禁用非成员函数的方法! :-) 以前我们能做的最好的事情就是声明而不定义它,将错误延迟到链接时间。
【解决方案2】:

另一个可能的用例, 未被该语言采用,但可能是 smart pointers constructor from raw pointer,例如:

// this is NOT the actual ctor in the language but could have been
// see explanation in above link
unique_ptr(T* const&& p) : ptr{p} {}

【讨论】:

    猜你喜欢
    • 2011-09-12
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    相关资源
    最近更新 更多