« first day (1772 days earlier)      last day (1081 days later) » 

8:15 AM
#include <iostream>
void Name(std::string&& s){
std::cout<<"Name Called"<<std::endl;
}
int main()
{
Name("Agent_A");
}
In the above code is it possible to access the data of s
In Function Out Of void Name's Scope
 
nwp
Are you sure you didn't mean std::cout << s;?
 
Yes
 
nwp
I'm not sure what you mean by "access the data of s".
 
I'm Passing An rvalue by const reference
Is it possible to access the string out of the function scope
 
nwp
I don't see any const reference.
std::cout accesses s outside the function's lexical scope.
Oh, you mean when passing s because it's operator <<(const std::string &). Sure, you can do that.
 
8:22 AM
Yeah I meant that
 
nwp
The thing that you shouldn't do is store a reference to the temporary. std::cout doesn't do that, but you might think that storing a struct S{ const std::string &s; } made from s is ok.
 
Oh ohk
 
 
5 hours later…
1:23 PM
typedef struct
{
std::vector<int> vecNum;
}fooSt;

int main()
{
std::map<int, fooSt> map2fooSt ={{0,{1,2.3}}};
const std::vector<int>& vec = map2fooSt[0].vecNum;
}
Does const std::vector<int>& vec = map2fooSt[0].vecNum need to copy the elements which is stored in std::vector<int>? Why?
It seems there no need to do so.
 
nwp
There would not be a copy.
Your typedef is very C.
 
@nwp Why?Could you please explain that in more detail for me or suggest some document for me to go through?
I can draw that conclusion that there is no copy by this code snippet(godbolt.org/z/WqEzfvnKM).But I don't fully understand it.
 
nwp
std::map::operator[] returns a reference. That's the only critical point as far as I can see. The rest is just accessing a member of a struct.
 
@John Could you share why you think there should be a copy?
 
I see. std::map::operator[] returns a reference indeed. What confuses me is that map2fooSt[0].vecNum queries a subfield of the structure named as fooSt.
 
1:57 PM
map2fooSt[0] returns an lvalue reference, so it is an lvalue expression. And referring to a member of an lvalue (the .vecNum part) is also an lvalue expression. This is the same value category as naming an object by its identifier, for example map2fooSt; would be an lvalue reference. Just naming an object does not copy it.
 

« first day (1772 days earlier)      last day (1081 days later) »