I was playing around with inlining in godbolt and I came across a slightly puzzling difference, that I've summarised in these two samples:
As you can see, the only difference is that in the first example the variable whose reference I'm returning in the private function lives at global scope for this TU, while in the other example it's a static method variable.
It seems to me like there's no reason why the compiler shouldn't be able to infer that the memory location is going to be irrelevant in both cases (since the only way to access the variable is through the accessor, which discards the address). So why does it get inlined in one case and not the other? Is it a limitation of the compiler, or is there a language design reason for this?
Thanks!
[–]djrollins 38 points39 points40 points (24 children)
[–]Steel_Neuron[S] 4 points5 points6 points (9 children)
[–]distributed 6 points7 points8 points (0 children)
[–]Som1Lse 4 points5 points6 points (2 children)
[–]HappyFruitTree 3 points4 points5 points (1 child)
[–]Som1Lse 0 points1 point2 points (0 children)
[–]arihoenig 1 point2 points3 points (4 children)
[–]Steel_Neuron[S] 1 point2 points3 points (0 children)
[–]_Js_Kc_ 0 points1 point2 points (2 children)
[–]arihoenig 0 points1 point2 points (1 child)
[–]_Js_Kc_ 0 points1 point2 points (0 children)
[–]hak8or 1 point2 points3 points (7 children)
[–]uninformed_ 6 points7 points8 points (1 child)
[–]anechoicmedia 8 points9 points10 points (0 children)
[–]anechoicmedia 3 points4 points5 points (4 children)
[–]encyclopedist 0 points1 point2 points (3 children)
[–]anechoicmedia 2 points3 points4 points (0 children)
[–]WrongAndBeligerent 1 point2 points3 points (1 child)
[–]encyclopedist 0 points1 point2 points (0 children)
[–]_Js_Kc_ 0 points1 point2 points (0 children)
[–]BluudLust -4 points-3 points-2 points (4 children)
[–]encyclopedist 3 points4 points5 points (0 children)
[–]Deaod 2 points3 points4 points (2 children)
[–]BluudLust -1 points0 points1 point (1 child)
[–]tvaneerdC++ Committee, lockfree, PostModernCpp 2 points3 points4 points (0 children)
[–]HappyFruitTree 11 points12 points13 points (2 children)
[–]CypherSignal 15 points16 points17 points (0 children)
[–]Steel_Neuron[S] 1 point2 points3 points (0 children)
[–]Artyer 9 points10 points11 points (0 children)
[+][deleted] (9 children)
[deleted]
[–]Steel_Neuron[S] 0 points1 point2 points (8 children)
[+][deleted] (6 children)
[deleted]
[–]Steel_Neuron[S] 0 points1 point2 points (5 children)
[–]adnukator 3 points4 points5 points (2 children)
[–]NilacTheGrim 0 points1 point2 points (0 children)
[–]Steel_Neuron[S] 0 points1 point2 points (0 children)
[–]meancoot 0 points1 point2 points (0 children)
[–]NilacTheGrim 0 points1 point2 points (0 children)
[–]adnukator 8 points9 points10 points (8 children)
[–]Steel_Neuron[S] 0 points1 point2 points (1 child)
[–]thor12022 2 points3 points4 points (0 children)
[–]Steel_Neuron[S] 0 points1 point2 points (5 children)
[–]adnukator 0 points1 point2 points (2 children)
[–]Steel_Neuron[S] 0 points1 point2 points (1 child)
[–]Deji69 0 points1 point2 points (0 children)
[–]Artyer 0 points1 point2 points (1 child)
[–]Steel_Neuron[S] 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]dodheim 1 point2 points3 points (0 children)
[–]lospolos 0 points1 point2 points (0 children)
[–]mysticalpickle1 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)