title: How to solve the issue of "read of non-constexpr variable 'a' is not allowed in a constant expression" with boost.hana
body: <p>I'm using c++17 with Boost.hana to write some meta-programming programs. One issue stuck me is what kind of expression can be used in a constexpr context like static_assert. Here is an example:</p>
  
  <pre><code>#include &lt;boost/hana.hpp&gt;
  
  using namespace boost::hana::literals;
  
  template &lt;typename T&gt;
  class X {
  public:
      T data;
  
      constexpr explicit X(T x) : data(x) {}
  
      constexpr T getData() {
          return data;
      }
  };
  
  
  int main() {
      {   // test1
          auto x1 = X(1_c);
          static_assert(x1.data == 1_c);
          static_assert(x1.getData() == 1_c);
      }
      {   //test2.1
          auto x2 = X(boost::hana::make_tuple(1_c, 2_c));
          static_assert(x2.data[0_c] == 1_c);
  
          // static_assert(x2.getData()[0_c] == 1_c); // read of non-constexpr variable 'x2' is not allowed in a constant expression
      }
      {   //test2.2
          auto x2 = X(boost::hana::make_tuple(1_c, 2_c));
          auto data = x2.getData();
          static_assert(data[0_c] == 1_c);
      }
  }
  </code></pre>
  
  <p>First I write a class <em>X</em> with a field <em>data</em> and an accessor <em>getData()</em>. In the <em>main()</em>'s <em>test1</em> part, <em>x1.data</em> and <em>x1.getData()</em> behave same as I expected. But in the <em>test2</em> part, changing the argument to a boost::hana's tuple, <code>static_assert(x2.data[0_c] == 1_c)</code> still behaves fine but <code>static_assert(x2.getData()[0_c] == 1_c)</code> fails compilation, with error of '<em>read of non-constexpr variable 'x2' is not allowed in a constant expression</em>'. What weired is if I split <code>x2.getData()[0_c]</code> into <code>auto data = x2.getData();</code> and <code>static_assert(data[0_c] == 1_c);</code> it compiles fine again. I'd expect they behave the same. So can anyone help explain why <code>x2.getData()[0_c]</code> can not be used in static_assert in this example?</p>
  
  <p>To reproduce: clang++8.0 -I/path/to/hana-1.5.0/include -std=c++17 Test.cpp</p>