简体   繁体   English

Widget&& var1 = someWidget; 有问题吗?

[英]Does something wrong with Widget&& var1 = someWidget;?

recently, i began to learing something about universal reference https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers and it says that Widget&& var1 = someWidget; //here, "&&" means rvalue reference最近,我开始学习有关通用参考https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers 的知识,它说Widget&& var1 = someWidget; //here, "&&" means rvalue reference Widget&& var1 = someWidget; //here, "&&" means rvalue reference . Widget&& var1 = someWidget; //here, "&&" means rvalue reference i know that "&&" can be universal reference or rvalue reference,but i thought somewidget must be a lvalue, and there is no deduce here, so Widget&& && must be rvalue reference, and here is the question.我知道“&&”可以是通用引用或右值引用,但我认为somewidget必须是左值,这里没有推导,所以Widget&& &&必须是右值引用,问题来了。 can rvalue reference accept a lvalue, i have learnt that lvalue reference can accept lvalue, and const lvalue reference can accept rvalue, but rvalue reference can only accept rvalue, so is there something wrong with this statement?右值引用可以接受左值吗,我了解到左值引用可以接受左值,而const左值引用可以接受右值,但是右值引用只能接受右值,那么这个说法有问题吗? or something i miss或者我想念的东西

I found that blog post to be confusing.我发现那篇博客文章令人困惑。 I'd recommend reading a good chapter in a book or the standard itself instead of that post.我建议阅读一本书或标准本身的好章节,而不是那篇文章。 Part of the problem is that the blog post is 10 years old, so people were still thinking about the new features and how to teach them.部分问题在于博客文章已有 10 年历史,所以人们仍在思考新功能以及如何教授它们。 I don't think that code even compiles though.我认为该代码甚至无法编译。 I tested int a = 5; int &&b = a;我测试int a = 5; int &&b = a; int a = 5; int &&b = a; and got a compile-time error as expected.并按预期得到了编译时错误。 I think he meant something like widget{} by someWidget .我认为他的意思是someWidgetwidget{} int &&j = int{5}; compiles fine.编译得很好。

However, if you have a template function like void f(T &&t) , it can turn into an lvalue reference or an rvalue reference, depending on what is passed to it.但是,如果你有一个模板 function 像void f(T &&t) ,它可以变成左值引用或右值引用,具体取决于传递给它的内容。 The main point of that is to implement perfect forwarding.其要点是实现完美转发。 The old way to do that was to innumerate all possible combinations of arguments, which is exponential in the number of arguments taken.这样做的旧方法是计算 arguments 的所有可能组合,这是所采用的 arguments 数量的指数。 Now, you can write one function that can call other functions with as much efficiency as possible, using the fact something is an rvalue if it is one.现在,您可以编写一个 function ,它可以尽可能高效地调用其他函数,使用的事实是,如果它是一个,它就是一个右值。 Consider the following example:考虑以下示例:

struct W
{
   W(int &, int &) {}
};

struct X
{
   X(int const &, int &) {}
};

struct Y
{
   Y(int &, int const &) {}
};

struct Z
{
   Z(int const &, int const &) {}
};

template <typename T, typename A1, typename A2>
T* factory(A1 &&a1, A2 &&a2)
{
   return new T(std::forward<A1>(a1), std::forward<A2>(a2));
}

int main()
{
   int a = 4;
   b = 5;
   W *pw = factory<W>(a, b);
   X *px = factory<X>(2, b);
   Y *py = factory<Y>(a, 2);
   Z *pz = factory<Z>(2, 2);

   delete pw;
   delete px;
   delete py;
   delete pz;
}

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

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