title: Single / Compile time evaluation of custom type as argument (Implicit constexpr?)
body: <p>Given the following code:</p>
  <pre><code>for (int i = 0; i &lt; 100; i++) {
      myFunction(Color(1.f, 1.f, 0.f));
  }
  </code></pre>
  <p>The constructor of Color would be called 100 times, although it always evaluates to the same thing every time and myFunction does not create a copy (utilizing <code>const Color&amp;</code>).</p>
  <p>I have already taken the necessary steps to design Color to be constexpr friendly:</p>
  <pre><code>class Color {
  public:
      constexpr Color(float r, float g, float b, float a = 1.f) : red(r), green(g), blue(b), alpha(a) {
          if (r &gt; 1.f) { throw std::runtime_exception(&quot;...&quot;); }
          ...
      }
      ...
  };
  </code></pre>
  <p>But that does not seem to achieve what I would like it to achieve.</p>
  <p>Assuming I have no control over <code>myFunction</code>, is there some sort of way that Color could be designed in order to evaluate the object a single time, possibly at compile time and then simply have it be passed to the function every time?
  Basically the automatic conversion of the code above to the following:</p>
  <pre><code>for (int i = 0; i &lt; 100; i++) {
      constexpr Color myColor(1.f, 1.f, 0.f);
      myFunction(myColor);
  }
  </code></pre>
  <p>The above code works, and even detects invalid input at compile time!</p>
  <p>Of note could be that detecting whether this optimization is actually already performed has proven quite hard for me, as adding measures to detect it could very well prevent it from being optimized away. Maybe the compiler will actually optimize it in a majority of cases, who knows? (Well, I hope someone here does.) But even then, at least the compile time error detection doesn't appear to be present.</p>
  <p>I am using C++17 but of course I'm happy to hear solutions utilizing newer language features as well!</p>
  <p>Is there anything more I can do or am I just ultimately at the mercy of the compiler here?</p>