In C++ how can I extract members from an array and return an array of the member's type? : <pre><code>namespace detail
{
    template &lt;typename T, typename U&gt;
    Array&lt;U&gt; extract_(const Array&lt;T&gt;&amp; array, std::function&lt;U(const T&amp;)&gt; member)
    {
        Array&lt;U&gt; extracted;

        for (auto&amp; item : array)
            extracted += member(item);

        return extracted;
    }
}


#define extract(container, member) detail::extract_(container, \
    std::function&lt; typeof(typeof(container)::Type::element_type::member) (const typeof(container)::Type&amp;)&gt;( \
                   [&amp;](const typeof(container)::Type&amp; item){ return item-&gt;member; }))
</code></pre>

<p>So that's the algorithm I want to use to extract members from arrays. <code>Array&lt;T&gt;</code> is my homegrown array type, it handles bidness.</p>

<p>If you're allergic to macros, sorry, but it makes user code very clean.</p>

<p>What I want to do is if I have <code>Array&lt;Size&gt; sizeArray</code> with member <code>double Array&lt;Size&gt;::Length</code>, to be able to say <code>auto lengths = extract(sizeArray, Length);</code> and have <code>lengths</code> be of type <code>Array&lt;double&gt;</code>.</p>

<p>I already do something similar with</p>

<pre><code>namespace detail
{
    template &lt;typename T&gt;
    Array&lt;T&gt; filter_(const Array&lt;T&gt;&amp; array, std::function&lt;bool(const T&amp;)&gt; condition)
    {
        Array&lt;T&gt; filtered;

        for (auto&amp; item : array)
            if (condition(item)) filtered += item;

        return filtered;
    }
}


// this macro creates a capturing lambda, the item to check is called 'item'
#define filter(container, condition) detail::filter_(container, \
    std::function&lt;bool(const typeof(container)::Type&amp;)&gt;( \
                   [&amp;](const typeof(container)::Type&amp; item){ return condition; }))
</code></pre>

<p>I can do <code>auto turnedOff = filter(objects, !item-&gt;IsTurnedOn);</code> and it works really well, but it's returning the same type, so it's an easier task to solve.</p>
