a*a
2 楼
v*y
3 楼
这是我的理解,不对的请指出,谢谢
The usage of rvalue reference &&
Define a class
class MyClass{
public:
MyClass(){/*require for const MyClass cMyClass init. Without this default
constructor, const MyClass will have to be declared and defined as such:
const MyClass cMyClass=MyClass(). MyClass() here will call synthesized
constructor automatically generated by compiler. This synthesized
constructor can not directly initiate const. So following is illegal without
default constructor: const MyClass cMyClass;*/}
};
//define three overload function
void foo(MyClass &&){
std::cout<}//only binds to rvalue
void foo(MyClass &){
std::cout<}//only binds to lvalue. Can not refer to rvalue
void foo(const MyClass &){
std::cout<}//binds to lvalue and rvlalue with low level const
call your overload function
foo(MyClass());//MyClass() is a temporary unnamed variable, call rvalue foo(
MyClass&&)
MyClass myClass;//init a named lvalue myClass on stack
foo(myClass);//call lvalue foo(MyClass&)
const MyClass cMyClass; ;//init a named const lvalue myClass on stack
foo(cMyClass);//call const lvalue foo(cosnt MyClass&)
If you don’t define rvalue foo(MyClass &&), foo(MyClass()) can call foo
function with const lvalue parameters, foo(const MyClass &). It can not
call foo(MyClass&).
The reason is that initiation of parameter is equal to variable initiation.
It can not reference a literal or unnamed temporary rvalue object. Following
initiation of variable ref_obj is illegal.
MyClass &ref_obj=MyClass();//can not reference to a rvalue object
Const reference can refer to literals and rvalue object:
const MyClass &ref_obj=MyClass();//legal
foo(ref_obj);//call foo(const MyClass&)
Examples on literal
void bar ( int &){
std::cout<}
void bar (const int &){
std::cout<}
bar(10) call bar(cons tint &) not bar(int &)
void poo(std::string &){
std::cout<}
void poo(const std::string &){
std::cout<}
poo("C++ rocks!");//calls poo(const std::string &) NOT poo(std::string&)
However, if we use rvalue reference, literals can be refer by &&
Define:
void poo(std::string &&){
std::cout<}
poo("C++ rocks!");//it will call void poo(std::string &&). Without it , it
will call poo(const std::string&).
Note: for rvalue, it is default const, so std::string &&=const std::string &&
Then you wonder what will happen if you define a foo function with copy
initiation (not reference) as follows.
void foo(MyClass){
std::cout<}
For function with copy initiation, you can not overload with a top level
const.
This is because top level const is ignored during type matching. It looks
same to the compiler.
void foo(const MyClass){
std::cout<} //redefine foo (MyClass) can not overload
So you can only choose one. And following are able to call foo(MyClass)
foo(MyClass());//rvalue
MyClass myClass;
foo(myClass);//lvalue
const MyClass cMyClass;
foo(cMyClass);//const lvalue. Top level const is ignored in initiation
Following is very confusing. Foo copy initiation can be overloaded with foo
reference version. But usage will be ambiguous and cause compiler error.
It is no problem to define
void foo(MyClass){
std::cout<}//MyClass can binds to lvalue and rvalue
void foo(const MyClass &&){
std::cout<}//only binds to rvalue
void foo(MyClass &){
std::cout<}//binds to lvalue of course rvlalue
void foo(const MyClass &){
std::cout<}//binds to lvalue of course rvlalue with low level const
IF you call
foo(MyClass());//Compiler can not decide to call rvalue foo(MyClass&&) or
copy version foo(MyClass)
//It is equivalent
It is also true for the following
MyClass myClass;
foo(myClass);//ambiguous
const MyClass cMyClass;
foo(cMyClass);// ambiguous
The usage of rvalue reference &&
Define a class
class MyClass{
public:
MyClass(){/*require for const MyClass cMyClass init. Without this default
constructor, const MyClass will have to be declared and defined as such:
const MyClass cMyClass=MyClass(). MyClass() here will call synthesized
constructor automatically generated by compiler. This synthesized
constructor can not directly initiate const. So following is illegal without
default constructor: const MyClass cMyClass;*/}
};
//define three overload function
void foo(MyClass &&){
std::cout<}//only binds to rvalue
void foo(MyClass &){
std::cout<}//only binds to lvalue. Can not refer to rvalue
void foo(const MyClass &){
std::cout<}//binds to lvalue and rvlalue with low level const
call your overload function
foo(MyClass());//MyClass() is a temporary unnamed variable, call rvalue foo(
MyClass&&)
MyClass myClass;//init a named lvalue myClass on stack
foo(myClass);//call lvalue foo(MyClass&)
const MyClass cMyClass; ;//init a named const lvalue myClass on stack
foo(cMyClass);//call const lvalue foo(cosnt MyClass&)
If you don’t define rvalue foo(MyClass &&), foo(MyClass()) can call foo
function with const lvalue parameters, foo(const MyClass &). It can not
call foo(MyClass&).
The reason is that initiation of parameter is equal to variable initiation.
It can not reference a literal or unnamed temporary rvalue object. Following
initiation of variable ref_obj is illegal.
MyClass &ref_obj=MyClass();//can not reference to a rvalue object
Const reference can refer to literals and rvalue object:
const MyClass &ref_obj=MyClass();//legal
foo(ref_obj);//call foo(const MyClass&)
Examples on literal
void bar ( int &){
std::cout<}
void bar (const int &){
std::cout<}
bar(10) call bar(cons tint &) not bar(int &)
void poo(std::string &){
std::cout<}
void poo(const std::string &){
std::cout<}
poo("C++ rocks!");//calls poo(const std::string &) NOT poo(std::string&)
However, if we use rvalue reference, literals can be refer by &&
Define:
void poo(std::string &&){
std::cout<}
poo("C++ rocks!");//it will call void poo(std::string &&). Without it , it
will call poo(const std::string&).
Note: for rvalue, it is default const, so std::string &&=const std::string &&
Then you wonder what will happen if you define a foo function with copy
initiation (not reference) as follows.
void foo(MyClass){
std::cout<}
For function with copy initiation, you can not overload with a top level
const.
This is because top level const is ignored during type matching. It looks
same to the compiler.
void foo(const MyClass){
std::cout<} //redefine foo (MyClass) can not overload
So you can only choose one. And following are able to call foo(MyClass)
foo(MyClass());//rvalue
MyClass myClass;
foo(myClass);//lvalue
const MyClass cMyClass;
foo(cMyClass);//const lvalue. Top level const is ignored in initiation
Following is very confusing. Foo copy initiation can be overloaded with foo
reference version. But usage will be ambiguous and cause compiler error.
It is no problem to define
void foo(MyClass){
std::cout<}//MyClass can binds to lvalue and rvalue
void foo(const MyClass &&){
std::cout<}//only binds to rvalue
void foo(MyClass &){
std::cout<}//binds to lvalue of course rvlalue
void foo(const MyClass &){
std::cout<}//binds to lvalue of course rvlalue with low level const
IF you call
foo(MyClass());//Compiler can not decide to call rvalue foo(MyClass&&) or
copy version foo(MyClass)
//It is equivalent
It is also true for the following
MyClass myClass;
foo(myClass);//ambiguous
const MyClass cMyClass;
foo(cMyClass);// ambiguous
s*t
5 楼
周末IKEA去买了把搪瓷的想棍子一样的,还没试呢
c*o
7 楼
用碗底磨刀,推荐中式粗瓷饭碗。
相关阅读
h1b每年最长可以外派其他国家几个月?python3 输入 菜鸟问题王垠终于回国了 (转载)没有比Visual Studio更好的IDE了吧?Kaggle上有什么和finance/banking相关的比赛比较适合拿来秀的问一个很白痴的问题,config管理胡扯几句什么叫Deep Learning入门哪里找Wiki Leak希拉里Podesta邮件可视化项目AI应该首先被用来解数学难题CSS坑真多能在mac上挖矿吗?百度那个Apollo有人用过吗?ssh tunnel加密的问题觉得ML在business里面没多大作用uber和当年的apple有点象了,头头被政治斗争赶走了。谁能给个python 2 decorator class的例子?怎样买slickedit便宜?有人用react + redux么怎样能在网上找到计算机类的工作? (转载)会用c#/python program MSSql, Excel, 从网上抓数据,合适做啥工作?