Using a Function pointer inside a class crashes the compiler (but works inside a function)
<p>Reference class</p>

<pre><code>class commandsListClass
{
public:
    std::string name;
    std::string description;
    std::vector&lt;std::string&gt; commands;
    columnHeaders headersRequired;
    void (*function)(System::Object ^ );
    std::string recoveryFileHeader;
    void reset()
    {
        name = "";
        description = "";
        commands.clear();
        headersRequired.reset();
        recoveryFileHeader = "";
        function = dummyFunc; // dummyFunc uses the same members as the intended - this is to ensure it is defined. DummyFunc is empty, returns void etc
    }
    commandsListClass()
    {
        reset();
    }
};
</code></pre>

<p>Currently, if I run the below code, the compiler crashes   </p>

<pre><code>// This crashes the compiler
System::Threading::ThreadPool::QueueUserWorkItem(gcnew System::Threading::WaitCallback(global::commandsList[index].function ), ti);
</code></pre>

<p><a href="https://i.stack.imgur.com/EGbKj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EGbKj.png" alt="Visual of the crashed compiler"></a></p>

<pre><code>1&gt;------ Build started: Project: MyProject, Configuration: Release x64 ------
1&gt;  project.cpp
1&gt;c:\users\guy\documents\visual studio 2012\projects\MyProject\MyProject\Form1.h(807): fatal error C1001: An internal error has occurred in the compiler.
1&gt;  (compiler file 'msc1.cpp', line 1443)
1&gt;   To work around this problem, try simplifying or changing the program near the locations listed above.
1&gt;  Please choose the Technical Support command on the Visual C++ 
1&gt;   Help menu, or open the Technical Support help file for more information
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
</code></pre>

<p>If I declare a member inside the same function as I am making the call, and set it to the global::commandsList[index].function, it compiles and runs correctly</p>

<pre><code>// This runs correctly    
void (*func)(System::Object ^);
func = global::commandsList[index].function;
System::Threading::ThreadPool::QueueUserWorkItem(gcnew System::Threading::WaitCallback(func ), ti);
</code></pre>

<p>global::commandsList is a vector of type commandsListClass</p>

<p>Any ideas? Browsing Google and SO suggest changing the compiler to not optimize, which I've tried with no success. The code is written in such a way that:</p>

<ul>
<li>That point in the code cannot be reached if index does not point to a valid member of the global::commandsList vector</li>
<li>The function variable is guaranteed to be set, either to the dummyFunc on creation, or the correct (requested) function as set elsewhere in the code.</li>
</ul>

<p>Any help would be greatly appreciated.</p>

<p>Edit 1: This is using Visual Studio 2012, Windows 7 x64</p>