Dan Piponi on Nostr: Let me tell you about my embarrassingly stupid C++ mistake so you don't have to make ...
Let me tell you about my embarrassingly stupid C++ mistake so you don't have to make it:
If X is of type std::optional<T> and Y is of type T you can assign Y to X like so:
X = Y
I sort of don't like that because X and Y are different types.
You can also write
*X = Y
which works because *X is of type T&. Now the types match.
But woe is me! Don't do the latter. If X is an empty optional then *X = Y fails silently.
It's obvious when you think about it for a moment. But when you're thinking about the bigger picture you can forget about the little things.
Published at
2024-05-08 01:15:30Event JSON
{
"id": "f66adb246cf82750ea76f9a80c4735781072d69a54077350d66fcb8ed94c502d",
"pubkey": "3422fcbc32f333fb2d3481b2e981258af8a0b571869cbfe93c42962410e232ef",
"created_at": 1715130930,
"kind": 1,
"tags": [
[
"proxy",
"https://mathstodon.xyz/users/dpiponi/statuses/112402820645353859",
"activitypub"
]
],
"content": "Let me tell you about my embarrassingly stupid C++ mistake so you don't have to make it:\n\nIf X is of type std::optional\u003cT\u003e and Y is of type T you can assign Y to X like so:\n\nX = Y\n\nI sort of don't like that because X and Y are different types.\n\nYou can also write\n\n*X = Y\n\nwhich works because *X is of type T\u0026. Now the types match.\n\nBut woe is me! Don't do the latter. If X is an empty optional then *X = Y fails silently.\n\nIt's obvious when you think about it for a moment. But when you're thinking about the bigger picture you can forget about the little things.",
"sig": "525f38d93072ffe8ad2a4dd4d239b644ca1d8f1bf40e2c7d6c798357a684def1fd4abee16eb729e00d508f8bbecf0f38ab4493040501f47801aa4cfc2c8fc834"
}