title: Structured bindings for your own type that isn’t a struct or a tuple(via public member function)
body: <p>I am going through the Herb Sutter's </p>
  
  <p><strong>A journey: Toward more powerful and simpler C++ programming</strong></p>
  
  <p><a href="https://web.archive.org/web/20170708213601/https://herbsutter.com/2016/06/30/trip-report-summer-iso-c-standards-meeting-oulu/" rel="noreferrer">Structure Binding</a> section </p>
  
  <p>In order to understand the concept .Best is to write a program I tried but getting some error </p>
  
  <blockquote>
    <p>Just want to try how to use structure binding on class with private
    data .Please ignore the below example.if any example you can provide </p>
  </blockquote>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  using namespace std;
  
  class foobar {
  public:
      foobar() { cout &lt;&lt; "foobar::foobar()\n"; }
      ~foobar() { cout &lt;&lt; "foobar::~foobar()\n"; }
  
      foobar( const foobar &amp;rhs )
      { cout &lt;&lt; "foobar::foobar( const foobar &amp; )\n"; }
      void ival( int nval, string new_string ) { _ival = nval;s=new_string; }
  
  private:
      int _ival;
      string s;
  };
  
  foobar f( int val,string new_string ) {
      foobar local;
      local.ival( val,new_string );
      return local;
  }
  
  template&lt;&gt; struct tuple_element&lt;0,foobar&gt; { using type = int; };
  template&lt;&gt; struct tuple_element&lt;1,foobar&gt; { using type = string; };
  
  
  
   // 2. Now add get&lt;&gt; support (using C++17, because why not; it’s better
   // than =delete’ing the primary get&lt;&gt; template and adding specializations)
   template&lt;int I&gt;
   auto get(const foobar&amp;x) {
   if      constexpr(I == 0) return x._ival;//'_ival' is a private member of 'foobar'
   else if constexpr(I == 1) return x.s;//'s' is a private member of 'foobar'
   }
  
  
  int main(){
      foobar ml = f( 1024,"hello" );
      auto [ n, s] = f( 1024,"hello" );//Cannot decompose non-public member '_ival' o
      return 0;
  }
  </code></pre>
  
  <p>Error</p>
  
  <blockquote>
    <p>if      constexpr(I == 0) return x._ival;//'_ival' is a private member
    of 'foobar'</p>
    
    <p>else if constexpr(I == 1) return x.s;//'s' is a private member of 'foobar'</p>
    
    <p>auto [ n, s] = f( 1024,"hello" );//Cannot decompose non-public</p>
  </blockquote>
  
  <p><strong>Help required</strong></p>
  
  <p>1.If anyone can elaborate what he is actually trying to do on these lines 
  (please refer the link provided)</p>
  
  <pre><code>// 2. Now add get&lt;&gt; support (using C++17, because why not; it’s better
  // than =delete’ing the primary get&lt;&gt; template and adding specializations)
  template&lt;int I&gt;
  auto get(const S&amp;) {
     if      constexpr(I == 0) return x.i;
     else if constexpr(I == 1) return string_view{x.c}; }
     else if constexpr(I == 2) return x.d;
  }
  </code></pre>
  
  <p>2.Any suggestion how to fix the error for the above example</p>