c++ - 你会把 C++ RValue 引用参数标记为 const

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

我一直在切换模板工厂函数以使用(并理解)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;
}

No matching function for call to 'MakeMyPair_Forward' Candidate function [with T = int, U = bool] not viable: no known conversion from 'int' to 'const int &&' for 1st argument

这从 http://en.cppreference.com/w/cpp/utility/forward 是有道理的哪个状态 const 被推导并且我正在传递左值。

  • If a call to wrapper() passes an rvalue std::string, then T is deduced to std::string (not std::string&, const std::string&, or std::string&&), and std::forward ensures that an rvalue reference is passed to foo.
  • If a call to wrapper() passes a const lvalue std::string, then T is deduced to const std::string&, and std::forward ensures that a const lvalue reference is passed to foo.
  • If a call to wrapper() passes a non-const lvalue std::string, then T is deduced to std::string&, and std::forward ensures that a non-const lvalue reference is passed to 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

谢谢。

最佳答案

So, the question. Does it make any sense to mark an rvalue reference as const when passing as a parameter?

这是在 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&& 用于捕获所有右值,const 与否,并将它们扔到编译时错误中,同时允许左值,甚至 const 左值绑定(bind)和工作。

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

关于c++ - 你会把 C++ RValue 引用参数标记为 const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14742081/

相关文章:

c++ - 原始指针和智能指针混合函数重载

c++ - C 的 SOLID 原则实现

c++ - std::unique_ptr 带有用于 win32 LocalFree 的自定义删除器

c - 为什么两次计算给出不同的答案?

python - 使用命名元组作为事实上的常量——聪明还是愚蠢?

C++ 到函数式

php - 如何为 apache 守护进程设置 LD_LIBRARY_PATH?

c++ - 是否有可能获得 boost 区域设置边界分析以在撇号上进行拆分?

c++ - 避免调用默认、移动和复制构造函数

c++ - 使用 const 引用为变量添加别名