« first day (1564 days earlier)      last day (1280 days later) » 

7:44 AM
I found that I can't use a reference as a map value but instead I can use const reference as a map value.
map<int, ClassA&> myMap doesn't compile. While map<int, const ClassA&> is Ok.
Code sample:

https://wandbox.org/permlink/Te9b9SxNLzDqTboI
Wow, what surprised me is that no one mentions that we can use const reference as a map value in these 2 answers.
14
Q: C++: Is it possible to use a reference as the value in a map?

JonathanIs it possible to use a reference as the value in a standard map container in C++? If not - why not? Example declaration: map<int, SomeStruct&> map_num_to_struct; Example usage: ... SomeStruct* some_struct = new SomeStruct(); map_num_to_struct[3] = *some_struct; map_num_to_struct[3].some_fie...

8
Q: STL map containing references does not compile

Oliver ZhengThe following: std::map<int, ClassA &> test; gives: error C2101: '&' on constant While the following std::map<ClassA &, int> test; gives error C2528: '_First' : pointer to reference is illegal The latter seems like map cannot contain a reference for the key value, since it needs to in...

And these answers also indicate that even for const reference, it won't compile.
Any ideas guys?
 
 
2 hours later…
9:33 AM
you could always try to use it wrapped by std::ref , right?
 
Ron
9:44 AM
Why the condition_variable's notify_one needs a flag? Is it a global flag param?
 
@Ron which flag are you talking about?
 
Ron
my bad, I meant the .wait().
cv.wait(lck, [] { return isthisaglobalflag; });
How to have this code for all waiting threads? They all proceed when I have this in my waiting code. And in worker I have only cv.notify_one. But all waiting threads proceed.
 
That's just to check if the wake-up was spurious. Because on some platforms it's not efficient to make sure that wake-ups only happen when notify is called
just check if the thing you were waiting for, has actually happened
 
Ron
10:13 AM
Ah right, thanks.
 
11:11 AM
hi
 
 
2 hours later…
1:13 PM
1
Q: C++ map, use const reference as value type, what is the problem here?

RickToday I saw my boss's code which uses a const reference as a map's value type. Here's the code: class ConfigManager{ public: map<PB::Point, const PB::WorldPoint&, compare_point> world_point; //the rest are omitted }; (PB is Google Protobuf, we are using the Protobuf library. I do...

Check my question @PeterT if you have time. I found something else.
 
yeah, that's really odd, no idea what's going on there by taking a look
the issue might be with make_pair though
@Rick maybe myMap.try_emplace("love", a)); gets you some more consistent results
 
1:39 PM
you can say by looking at the address size (in bytes) what's the compiler arhitecture ? 32/ 64 bits?
 
yeah it used to be common to do sizeof(void*)
 
Ron
1:52 PM
When using std::thread t1(&SomeClass::fn, &object) + more threads doing the same, does every thread get a copy of an object?
 
no, not when you pass it as a pointer
then they'll all get a copy of the same pointer
 
Ron
When passed as pointer, are multiple threads sharing the object?
How is this managed: std::thread t1(&SomeClass::fn, object) ?
Every thread gets it copy of an object?
 
yeah, the process is detailed under 3) here: en.cppreference.com/w/cpp/thread/thread/thread
 
2:14 PM
Well I am checking an answer. Someone answered my question.
I don't understand why there is still someone downvoting my quesiton. Very funny. The question comes from real life and I've tried to rephrase it as best as I could. the question is clean with paragraphs separated.
 
It's a good question and well structured, some people are just weird about downvotes
 
can you align an entire struct?
 
:) Thank you. You made me less frustrated. I am going to check those answers.
 
or if you align the first element of a struct , all the struct get alligned accordingly
 
yeah you can use alignas in the struct definition too
 
2:23 PM
what happens if you have a struct like this
struct{
    char a;
    char alignas(short) b;
    char alignas(int) c;
}
how do you know how it will be aligned?
 
you check with alignof :P
 
I would say sizeof(struct) = 12
yes, I know but I don't understand the output
and instead it is 8
a and b are aligned 2 bytes and c is aligned 4 bytes
how comes this
why aren't all of them aligned to the biggest type ? (e.g int)
 
because that would just waste space
 
and which is the syntax to align the entire structure to defined type?
 
struct alignof(8) myStruct{
char a;
};
that aligns the struct itself, not every data-member of the struct if that's what you mean
 
2:34 PM
oh so that just padds bits at the end of the structure
not at the end of each member
 
yep
 
oh
bt regarding the previous example. why a and b are aligned at 2 bytes and only c is aligned at 4?
is there any rule that the alignment is taken into consideration from "nothing" to the first appearance and from there to the second appearance ?
 
 
1 hour later…
3:54 PM
@PeterT Well I don't understand the answer... stackoverflow.com/a/64483152/5983841
Could anyone help me understand with some baby-step explanation...?
@milleniumbug hello here :P
 
4:19 PM
I upvoted your question
it's interesting in that most SO answers talk about the case where the container's value_type is a reference type, but that's not what's happening in case of std::map
it's std::pair<const K, V>, and V happens to be a reference type, but value_type itself, is not one
my honest opinion on your code: please don't do this. While I have attempted, for about 20 minutes, to search the standard draft for words that would say it's wrong, I have failed to find anything relevant.
You encountering the problems mentioned in the question is the main reason why to not do this.
So anyway, while I can't speak, so far, of the legal aspects of what's going on, I can describe behaviour of your implementation.
> Isn't using a const reference can extend a temporary's lifetime?
this is a very specific rule that applies to very specific situation, that is
const T& a = my_temporary;
(if you ask me, we would be better off without this rule - but there is more confusing stuff in C++)
(by "please don't do this", I mean using references as mapped_type - I can't tell if it's legal, but it's definitely "it hurts when I do this")
 
4:58 PM
@Rick ^
 
I am here. midnight 1:00 :). Let me take a look at your messages
 
don't want to wake you up
 
No you didn't. I just took a shower and am trying to read some related stuff before I go to bed.
to understand it more
Anyway, let me read your messages. I read very slow.
Ok I got this.
Well, I really appreicated your help. You even spent 20 mins in looking for the standard.
@milleniumbug The answers are not merely talking about value_type I think. They are referring to std::pair<const K, V> the V
And certainly I would not do this. With any thing that need such efforts into research, I won't do it.
I just need some evidence or proof to convince my boss. Maybe tomorrow. He wrote the code and questioned me when I asked him about this.
 
5:36 PM
@milleniumbug Well, suddenly I understand this specific rule. Feels good
 
6:11 PM
@milleniumbug yes u r right. I was wrong. Some answers dont point out V in pairs directly, but seems to talk about value_tyoe
anyway, the exception rule still applies. I may modify the answer tomorrow. need to go to be right now
 
 
2 hours later…
8:29 PM
why does a directive cannot be produced as the result of macro expansion ?
 

« first day (1564 days earlier)      last day (1280 days later) »