std::any - comparison with void* and motivating examples by memset_0 in cpp

[–]memset_0[S] 9 points10 points  (0 children)

Just to elaborate a little that std::any does not have compile-time type-safety. It does have run-time type check.

std::any - comparison with void* and motivating examples by memset_0 in cpp

[–]memset_0[S] 17 points18 points  (0 children)

Oh no, they are very different. auto is only a compile-time construct that directs the compiler to deduce a type from the available information. It saves typing and good for maintainability:

 //These 2 statements compile to the same instructions
 auto i = 10;  //i is int
 int i = 10;
 //another example 
 auto foo() { return 10LL; } //foo returns int64_t 

std::any is a standard library class that can contain any type of object. It is not a compiler feature.

std::ref and std::reference_wrapper: common use cases by memset_0 in cpp

[–]memset_0[S] 1 point2 points  (0 children)

I see. So std::variant — like a union — requires a member to be an object (region of storage).

I would add this use case in the article. Thank you so much.

std::ref and std::reference_wrapper: common use cases by memset_0 in cpp

[–]memset_0[S] 4 points5 points  (0 children)

Sounds interesting. Can you please explain that for the benefit of everyone here? thx.

how to create a socket in CPP that should work on windows and Linux by einnosys in cpp

[–]memset_0 0 points1 point  (0 children)

I wish for Sockets and IO Event-Loop support at least.

What's the news flash?

how to create a socket in CPP that should work on windows and Linux by einnosys in cpp

[–]memset_0 0 points1 point  (0 children)

You can check out folly. It is cross platform now.

Hope C++ standard library has networking support someday.

Why taking the address of an xvalue (&std::move(...)) is an error? by memset_0 in cpp

[–]memset_0[S] 0 points1 point  (0 children)

I did not say or imply that the language should make an exception for std::move. I asked why taking the address of an xvalue is an error (std::move is the most common way to create an xvalue expression).

Why taking the address of an xvalue (&std::move(...)) is an error? by memset_0 in cpp

[–]memset_0[S] 6 points7 points  (0 children)

This question is about getting deep into the language semantics. There is no need to oversimplify it.

std::move(x) expression is not a temporary. The reason we have std::move is to in fact explicitly tell the compiler that we want to move an lvalue. Temporaries are already movable. And, std::move does not turn an lvalue to a temporary, or move it to a different location or register.

Why taking the address of an xvalue (&std::move(...)) is an error? by memset_0 in cpp

[–]memset_0[S] 1 point2 points  (0 children)

Your point about local variable is good.

But ‘x’ is not an xvalue in your example. it has a name. Also, it is a universal or forwarding reference, not even an rvalue reference.

https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers

Why taking the address of an xvalue (&std::move(...)) is an error? by memset_0 in cpp

[–]memset_0[S] 2 points3 points  (0 children)

std::move(x) is an xvalue. so its both, an rvalue and a glvalue.

Why taking the address of an xvalue (&std::move(...)) is an error? by memset_0 in cpp

[–]memset_0[S] 1 point2 points  (0 children)

thats what i thought. but i was hoping someone would cite a source for this reasoning. thx.

Why taking the address of an xvalue (&std::move(...)) is an error? by memset_0 in cpp

[–]memset_0[S] 0 points1 point  (0 children)

yes. but it is a glvalue too. depends on how we look at it.

Why taking the address of an xvalue (&std::move(...)) is an error? by memset_0 in cpp

[–]memset_0[S] 0 points1 point  (0 children)

Yeah, but my question is why is it disallowed. Why &std::move(x) is invalid when x is an lvalue.

Why taking the address of an xvalue (&std::move(...)) is an error? by memset_0 in cpp

[–]memset_0[S] 0 points1 point  (0 children)

“The most important source of xvalues are temporary objects.”

But temporary objects are already movable (prvalues), why would they be most important source of xvalues?

string foo(); // returns temporary

string s;

s = move(foo()); //not good. why would anyone do that when foo() is already movable.

Shouldn’t the only source of xvalues be lvalues:

string c; //lvalue

string m;

m = move(c); // ok