title: struct with const member
body: <p>I'm having some trouble with a template struct.</p>
  
  <pre><code>template&lt;typename T&gt;
  struct A{
      const int v1;
      T* v2;
  };
  </code></pre>
  
  <p>My purpose is to make <code>v1</code> always not editable, while <code>v2</code> should be editable if I use <code>T</code> and not editable if I use <code>const T</code> as type.</p>
  
  <p>If I create constructor to initialize <code>A</code>, the struct becomes:</p>
  
  <pre><code>template&lt;typename T&gt;
  struct A{
      const int v1;
      T* v2;
      A() : v1(-1) { v2=NULL; }
      A(int value) : v1(value) { v2=NULL; }
  };
  </code></pre>
  
  <p>But then g++ says that I need a specific assignment operator:</p>
  
  <blockquote>
    <p>error: non-static const member ‘const int A::v1’, can’t use default assignment operator</p>
  </blockquote>
  
  <p>But my assignment operator should also allow editing of <code>v1</code>. The only thing I'd like to avoid is an edit from the outside, something like:</p>
  
  <pre><code>A a;
  a.v1=10;
  </code></pre>
  
  <p>Is there any way to implement this (without creating getter/setter or using a pointer to a new A(int) with the desired value)?</p>
  
  <p>What if I declare v1 as a const int * ? It could refer someway to some value, but it cannot edit it.</p>