c++ 源码中&&变量是什么意思呢?

芥末的无奈 2013-12-15 04:37:56
源码中经常见到类似 _Valty&& _Val 这样的参数,这种变量是什么意思?
...全文
647 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
shiguojie19892 2013-12-17
  • 打赏
  • 举报
回复
引用 18 楼 lpcads 的回复:
右值引用。 举个例子

//需要一次构造,两次赋值,即三次深拷贝
swap(T &a,T &b)
{
	T tmp(a);
	a = b;
	b = tmp;
}
使用右值引用后只需三次浅拷贝就能圆满完成任务。
请给写一下右值引用在此的用法。
mujiok2003 2013-12-16
  • 打赏
  • 举报
回复
rvalue reference
赵4老师 2013-12-16
  • 打赏
  • 举报
回复
引用 15 楼 qq120848369 的回复:
右值引用,当传入临时对象时可以避免一次拷贝。
学习了……
qq120848369 2013-12-16
  • 打赏
  • 举报
回复
右值引用,当传入临时对象时可以避免一次拷贝。
jiandingzhe 2013-12-16
  • 打赏
  • 举报
回复
引用 13 楼 shiguojie19892 的回复:
[quote=引用 6 楼 iihero 的回复:] 并且的意思。 逻辑与
对的[/quote] 对个毛!!参数声明怎么个逻辑与。
shiguojie19892 2013-12-16
  • 打赏
  • 举报
回复
引用 6 楼 iihero 的回复:
并且的意思。 逻辑与
对的
赵4老师 2013-12-16
  • 打赏
  • 举报
回复
//C++ Operators
//  Operators specify an evaluation to be performed on one of the following:
//    One operand (unary operator)
//    Two operands (binary operator)
//    Three operands (ternary operator)
//  The C++ language includes all C operators and adds several new operators.
//  Table 1.1 lists the operators available in Microsoft C++.
//  Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators.  Operators associate with either the
//expression on their left or the expression on their right;    this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
//  Table 1.1 shows the precedence and associativity of C++ operators
//  (from highest to lowest precedence).
//
//Table 1.1   C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator         | Name or Meaning                         | Associativity |
//+------------------+-----------------------------------------+---------------+
//| ::               | Scope resolution                        | None          |
//| ::               | Global                                  | None          |
//| [ ]              | Array subscript                         | Left to right |
//| ( )              | Function call                           | Left to right |
//| ( )              | Conversion                              | None          |
//| .                | Member selection (object)               | Left to right |
//| ->               | Member selection (pointer)              | Left to right |
//| ++               | Postfix increment                       | None          |
//| --               | Postfix decrement                       | None          |
//| new              | Allocate object                         | None          |
//| delete           | Deallocate object                       | None          |
//| delete[ ]        | Deallocate object                       | None          |
//| ++               | Prefix increment                        | None          |
//| --               | Prefix decrement                        | None          |
//| *                | Dereference                             | None          |
//| &                | Address-of                              | None          |
//| +                | Unary plus                              | None          |
//| -                | Arithmetic negation (unary)             | None          |
//| !                | Logical NOT                             | None          |
//| ~                | Bitwise complement                      | None          |
//| sizeof           | Size of object                          | None          |
//| sizeof ( )       | Size of type                            | None          |
//| typeid( )        | type name                               | None          |
//| (type)           | Type cast (conversion)                  | Right to left |
//| const_cast       | Type cast (conversion)                  | None          |
//| dynamic_cast     | Type cast (conversion)                  | None          |
//| reinterpret_cast | Type cast (conversion)                  | None          |
//| static_cast      | Type cast (conversion)                  | None          |
//| .*               | Apply pointer to class member (objects) | Left to right |
//| ->*              | Dereference pointer to class member     | Left to right |
//| *                | Multiplication                          | Left to right |
//| /                | Division                                | Left to right |
//| %                | Remainder (modulus)                     | Left to right |
//| +                | Addition                                | Left to right |
//| -                | Subtraction                             | Left to right |
//| <<               | Left shift                              | Left to right |
//| >>               | Right shift                             | Left to right |
//| <                | Less than                               | Left to right |
//| >                | Greater than                            | Left to right |
//| <=               | Less than or equal to                   | Left to right |
//| >=               | Greater than or equal to                | Left to right |
//| ==               | Equality                                | Left to right |
//| !=               | Inequality                              | Left to right |
//| &                | Bitwise AND                             | Left to right |
//| ^                | Bitwise exclusive OR                    | Left to right |
//| |                | Bitwise OR                              | Left to right |
//| &&               | Logical AND                             | Left to right |
//| ||               | Logical OR                              | Left to right |
//| e1?e2:e3         | Conditional                             | Right to left |
//| =                | Assignment                              | Right to left |
//| *=               | Multiplication assignment               | Right to left |
//| /=               | Division assignment                     | Right to left |
//| %=               | Modulus assignment                      | Right to left |
//| +=               | Addition assignment                     | Right to left |
//| -=               | Subtraction assignment                  | Right to left |
//| <<=              | Left-shift assignment                   | Right to left |
//| >>=              | Right-shift assignment                  | Right to left |
//| &=               | Bitwise AND assignment                  | Right to left |
//| |=               | Bitwise inclusive OR assignment         | Right to left |
//| ^=               | Bitwise exclusive OR assignment         | Right to left |
//| ,                | Comma                                   | Left to right |
//+------------------+-----------------------------------------+---------------+
unituniverse2 2013-12-16
  • 打赏
  • 举报
回复
引用 18 楼 lpcads 的回复:
右值引用。 举个例子

//需要一次构造,两次赋值,即三次深拷贝
swap(T &a,T &b)
{
	T tmp(a);
	a = b;
	b = tmp;
}
使用右值引用后只需三次浅拷贝就能圆满完成任务。
这个例子调用的仍然是拷贝,因为a、b和tmp均为具名的变量,按右值定义,这种情况a、b和tmp都是左值。 要使用右值版本的拷贝构造和赋值话(即移动构造和移动赋值),应改成下面的样子:

swap(T & a,T & b)
{
	T tmp(std::move<T>(a));
	a = std::move<T>(b);
	b = std::move<T>(tmp);
}
unituniverse2 2013-12-16
  • 打赏
  • 举报
回复
class A {}; void foo(const A &) {}; // 函数1 void foo(A &&) {}; // 函数2 ... A a; const A ac; foo(ac);// 函数1将被调用 foo(a);// 函数1将被调用 foo(A());// 函数2将被调用 foo(std::move(a));// 函数2将被调用
unituniverse2 2013-12-16
  • 打赏
  • 举报
回复
引用 21 楼 It_way 的回复:
[quote=引用 15 楼 qq120848369 的回复:] 右值引用,当传入临时对象时可以避免一次拷贝。
不是这样的吧,这是一个重载函数:
    _Myt& operator=(const _Valty& _Val)  
        {   // insert into container and increment stored iterator  
        iter = container->insert(iter, _Val);  
        ++iter;  
        return (*this);  
        }  
  
    _Myt& operator=(_Valty&& _Val)  
        {   // push value into container  
        iter = container->insert(iter, _STD forward<_Valty>(_Val));  
        ++iter;  
        return (*this);  
        }  
  
只用传递常量值时才会调用右值引用的方法,临时变量应该是调用第一种方法吧 [/quote] 临时变量就是调用第二种的(所以才叫右值)。C++11中右值的定义就是非具名(指的是变量名,不是类型名)临时变量
滴答滴答D 2013-12-16
  • 打赏
  • 举报
回复
引用 15 楼 qq120848369 的回复:
右值引用,当传入临时对象时可以避免一次拷贝。
不是这样的吧,这是一个重载函数:
    _Myt& operator=(const _Valty& _Val)  
        {   // insert into container and increment stored iterator  
        iter = container->insert(iter, _Val);  
        ++iter;  
        return (*this);  
        }  
  
    _Myt& operator=(_Valty&& _Val)  
        {   // push value into container  
        iter = container->insert(iter, _STD forward<_Valty>(_Val));  
        ++iter;  
        return (*this);  
        }  
  
只用传递常量值时才会调用右值引用的方法,临时变量应该是调用第一种方法吧
翅膀又硬了 2013-12-16
  • 打赏
  • 举报
回复
引用 7 楼 taodm 的回复:
C++2011标准的 右值引用 语法,没听说的需要去补课啦,要与时俱进啦。
与时俱进
xu6148152 2013-12-16
  • 打赏
  • 举报
回复
确实是右值引用,C++11的新特性。
lpcads 2013-12-16
  • 打赏
  • 举报
回复
右值引用。 举个例子

//需要一次构造,两次赋值,即三次深拷贝
swap(T &a,T &b)
{
	T tmp(a);
	a = b;
	b = tmp;
}
使用右值引用后只需三次浅拷贝就能圆满完成任务。
lm_whales 2013-12-15
  • 打赏
  • 举报
回复
引用 7 楼 taodm 的回复:
C++2011标准的 右值引用 语法,没听说的需要去补课啦,要与时俱进啦。
++
熊熊大叔 2013-12-15
  • 打赏
  • 举报
回复
&& 就是其他语言里的and
xyzinfernity 2013-12-15
  • 打赏
  • 举报
回复
去搜索“c++11右值引用”
芥末的无奈 2013-12-15
  • 打赏
  • 举报
回复
引用 7 楼 taodm 的回复:
C++2011标准的 右值引用 语法,没听说的需要去补课啦,要与时俱进啦。
原来叫这个啊,完全没听说啊。去补课了。
taodm 2013-12-15
  • 打赏
  • 举报
回复
C++2011标准的 右值引用 语法,没听说的需要去补课啦,要与时俱进啦。
iihero_ 2013-12-15
  • 打赏
  • 举报
回复
并且的意思。 逻辑与
加载更多回复(5)

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧