« first day (1565 days earlier)      last day (1271 days later) » 

1:34 AM
@Rick The proof that it doesn't work is simply that the objects presumably referred by your map have different addresses than the original object. I've provided such an example in my first comment to your question.
Or is the issue that it is presumed that the materialized temporaries have their lifetimes extended by the map?
 
2:05 AM
@FrançoisAndrieux Yes you are correct
I didn't say there's anything wrong with your answer.
I just think it would be better to emphasize "it's std::pair<const K, V>, and V happens to be a reference type, but value_type itself, is not one" mentioned above.
@FrançoisAndrieux And "the objects presumably referred by your map have different addresses than the original object." is not the main problem I think. The biggest problem is that the object pointed by the reference is destructed immediately. So does the memory is freed.
I haven't chosen to accepted an answer just because I wanna see if there are better answers ;P. Nothing wrong with your answer again. And thank you sincerely :)
 
If the goal is not have the map refer to the original object, then I do not see the purpose of trying to put a reference as the value type in the first place.
But in any case, I don't think the fact that value_type is not a reference is relevant.
If you tried to have a std::vector<const T &> and you got it to compile, that vector could never be in a position to extend the duration of any temporary assigned to its elements.
Reference or not, the container can't be used to preserve temporaries like this.
 
2:28 AM
@FrançoisAndrieux True
 
2:40 AM
Btw, I don't know much about materialized..
@FrançoisAndrieux one more word, it seems using std::ref can make everythign work as intended?
myMap.insert(make_pair("love", std::ref(a)));
 
make_pair yields std::pair<const char *, std::reference_wrapper<A>> so the pair contains a "reference" to a instead of a copy. Then the map's element is initialized to refer to the original A. When the temporary (the pair) is destroyed, its just a reference that is destroyed.
std::reference_wrapper is a container-friendly emulation of a reference, closer to a pointer.
Though you should use probably use std::map<std::string, std::reference_wrapper<A>> even if it works without it.
 
@FrançoisAndrieux Ok I get the idea
I see people saying that "DO NOT look reference as the same as pointer!". However, I really found it helpful to understand many "magics" e.g. the problem of my question and why adding a std::ref works here), if I look references as pointers.
 
 
4 hours later…
Ron
7:15 AM
How to call a thread on a dynamically allocated object of a class? std::thread t1(&MyClass::fn,o,param) where o was new-ed in main.
 
@Ron sure, that should work
if "fn" is overloaded you just might need to cast it to the right type to manually do overload resolution
 
 
3 hours later…
Ron
9:57 AM
Is there a way to ensure the writing to a file with std::fstream's flush?
 
not really, kind of platform dependent and even filesystem dependent
 
Ron
Does closing a fstream ensure a file writing?
 
well again, for the purposes of C++ it will consider the file "written to" and can be observed as such from that thread.

However I don't think you can rely on it in the sense of "if I pull out the USB stick now, I can guarantee the file write is complete"
since the C++ standard doesn't mandate how side effects are observable from other processes, since the concept of a "process" is not even a thing in the standard
so if you want to guarantee that, you need to look for the platform specific behavior or for a library that guarantees what you want
 
Ron
I see. Thanks.
 
Ron
10:24 AM
Why using std::this_thread::get_id() sometimes gets same values for different threads where the threads are spawned with: std::thread t([&]() { o.logMessage(s); }); O being an object of a class.
The std::this_thread::get_id() is called inside a class member function.
 
@Ron if one thread already ended another one can resuse the same id
 
Ron
11:25 AM
The threads seem to be spawned in sequence and not interfering with each other.
 
if you are destroying the thread object without detaching then the destructor will wait for the thread to finish
 
Ron
I see. All the threads are spawned in main()
And the object is created there also.
I reuse the object for all threads.
std::thread t1([&]() { o.memberFn(s); }); followed by another std::thread t2([&]() { o.memberFn(s); });
 
with "thread object" I meant "t1" and "t2", not "o"
 
Ron
I see. I simply join them in the main() thread afterwards.
 
if memberFn doesn't take a significant time, then the start-up time of a thread might make them essentially sequential
so to see if they actually run simultaneously you could add in a sleep in that function
 
Ron
12:03 PM
Ah, right, got it now.
 
 
4 hours later…
4:17 PM
If I have types T1<A> and T2<A>, and I want a template that takes in either type as an argument. Is it possible to access A?
template <typename A, typename T> f(T<A> t) {} does not work.
 
4:45 PM
@Argon I looked it up and apparently this is the unholy syntax for that:

template<typename A, template<typename> typename T>
void f(T<A> t)
 
2 messages moved from Lounge<C++>
 
@PeterT Thanks. How did you manage to find it?
 
'@Argon This syntax is known as template template parameter
 
I vaguely remembered there being "template templates", so I googled that
 
Thanks guys!
 
 
1 hour later…
6:05 PM
I know all people saying that in c++ it doesn't matter main() vs main(void), but in C the main() should be avoided as it means "any number of arguments" instead of "no arguments"
I don't get why is that a wrong answer
 
@PeterT By the way, can the template be made to infer if it is being passed T1 or T2 without having to pass them as a second template argument in the <>?
 
6:40 PM
@CătălinaSîrbu They are equivalent in C++. If the question is targeted at C++ then the correct answer is the first one.
 
either way C or C++ question it still looks wrong for me
 
@Argon It seems to work fine like this.
@CătălinaSîrbu The main argument I see in favor of int main() is that it is discouraged from needlessly adding the void parameter to function in general. If it was just a choice between the former or the latter I would prefer the latter, but not "highly" as the answer suggests. But because there is a "are the same" option, in my opinion not choosing that answer would be incorrect. Because they are identical except for a stylistic choice.
 
but wat if that is a C question ?
I don't know where is that picture from
 
In C the main functions are either int main(void), int main(int, char*[]) or implementation defined signatures. So the correct answer is int main(void) in C. Edit : I wouldn't even say it is "highly recommanded". I would say it just is the correct way to write it.
 
7:19 PM
thanks
 
 
3 hours later…
10:01 PM
the values stored on the stack (cpu stack for function calls) can be accesed as arrays? Or only LIFO ?
" Additionally, values stored on the stack may be accessed in the traditional way like items in an array, as there is a dedicated processor register that stores the address of the top-most element. If you know the address of the top of the stack, you can access all the values lying below."
 
 
2 hours later…
11:33 PM
@CătălinaSîrbu That is not true for C++.
That sounds like something you might do in assembly.
 

« first day (1565 days earlier)      last day (1271 days later) »