Going faster by duplicating code
IMO the reason the compiler doesn't add special cases for the simplest version is that it doesn't know which of its _many_ special cases to use. If you actually use the unoptimised version of the code like<p><pre><code>    void withSwitch(vector<int>& Values, bool v) {
      if (v) {
        multiply1(Values, 2.0);
      } else {
        multiply1(Values, 3.0);
      }
    }
</code></pre>
Then it actually inlines the code and optimises each one correctly, as it has context about which special cases are available. (Doesn't even need the `inline` keyword for this at `-O2`)<p>You can see the code here: <a href="https://godbolt.org/z/5beeYe77a" rel="nofollow noreferrer">https://godbolt.org/z/5beeYe77a</a>