Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Without seeing the definition of <code>MyClass</code> the problem is not quite clear. I believe anyway that you want to get <code>MyClass::_myVar</code> initialized without code to iteratively fill it with the <code>MyClass::metaFunction()</code> values.</p> <p>You code suggests that <code>MyClass::_myVar</code> is a static class member. In that case your initialization of the member is perfectly good C++11 as it stands. The following program illustrates (GCC 4.6.3):</p> <pre><code>#include &lt;iostream&gt; /* MyClass Version 1 */ template&lt;unsigned int DIM&gt; struct MyClass { static constexpr unsigned int metaFunction( const unsigned int k, const unsigned int n, const unsigned int dim); static const unsigned int _myVar[2][3]; }; template&lt;unsigned int DIM&gt; inline constexpr unsigned int MyClass&lt;DIM&gt;::metaFunction( const unsigned int k, const unsigned int n, const unsigned int dim) { return (((n &lt; dim) &amp;&amp; (k &lt; n)) ? (1&lt;&lt;(n-k)) : (0)); } template&lt;unsigned int DIM&gt; const unsigned int MyClass&lt;DIM&gt;::_myVar[2][3] = { { metaFunction(0, 0, DIM), metaFunction(0, 1, DIM), metaFunction(0, 2, DIM) }, { metaFunction(1, 0, DIM), metaFunction(1, 1, DIM), metaFunction(1, 2, DIM) } }; template&lt;unsigned int DIM&gt; inline constexpr unsigned int MyClass&lt;DIM&gt;::metaFunction( const unsigned int k, const unsigned int n, const unsigned int dim) { return (((n &lt; dim) &amp;&amp; (k &lt; n)) ? (1&lt;&lt;(n-k)) : (0)); } using namespace std; int main(void) { MyClass&lt;3&gt; mine; for (int i = 0; i &lt; 2; ++i) { for (int j = 0; j &lt; 3; ++j) { cout &lt;&lt; mine._myVar[i][j] &lt;&lt; endl; } } return 0; } </code></pre> <p>This inclines me to think that <code>MyClass::_myVar</code> is <em>not</em> a static member - although why this array of integer constants would not be static I am not sure. If that is the case, then you can initialize the member in the default constructor using the <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Uniform_initialization" rel="nofollow">uniform initialization provision of C++11</a>:</p> <pre><code>/* MyClass Version 2 */ template&lt;unsigned int DIM&gt; struct MyClass { MyClass() : _myVar{ { MyClass::metaFunction(0, 0, DIM), MyClass::metaFunction(0, 1, DIM), MyClass::metaFunction(0, 2, DIM) }, { MyClass::metaFunction(1, 0, DIM), MyClass::metaFunction(1, 1, DIM), MyClass::metaFunction(1, 2, DIM) } }{} static constexpr unsigned int metaFunction( const unsigned int k, const unsigned int n, const unsigned int dim); const unsigned int _myVar[2][3]; }; </code></pre> <p>In neither case is the <code>constexpr</code> attribute of <code>metaFunction</code> actually necessary for compilation. And if <code>constexpr</code> is removed then <code>/* MyClass Version 1*/</code> is also good for C++03.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload