<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9 Forums - Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
	<atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Forums/rss"></atom:link>
	<image>
		<url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
		<title>Channel 9 Forums - Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<link>http://channel9.msdn.com/Forums</link>
	</image>
	<description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
	<link>http://channel9.msdn.com/Forums</link>
	<language>en</language>
	<pubDate>Sun, 19 May 2013 08:52:53 GMT</pubDate>
	<lastBuildDate>Sun, 19 May 2013 08:52:53 GMT</lastBuildDate>
	<generator>Rev9</generator>
	<c9:totalResults>34</c9:totalResults>
	<c9:pageCount>-34</c9:pageCount>
	<c9:pageSize>-1</c9:pageSize>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>Hello fellow C&#43;&#43;0x programmers.</p><p>&nbsp;</p><p>I have this code:</p><p><pre class="brush: cpp">void normal_func(int p)
{
    _tprintf_s(_T(&quot;p: %d\n&quot;), p);
}

struct normal_struct_func
{
    inline void operator()(int p) const
    {
        _tprintf_s(_T(&quot;p: %d\n&quot;), p);
    }
};

template&lt;typename T, typename Function&gt;
void test_f(T p, Function func)
{
    func(p);
}

template&lt;typename T, typename Function&gt;
class test_c {
public:
    explicit test_c(T p, Function func) : m_p(p), m_Func(func) {}

    ~test_c(void)
    {
        m_Func(m_p);
    }

    T get(void) const
    {
        return m_p;
    }

private:
    T m_p;
    const Function m_Func;
};

int _tmain(int argc, _TCHAR* argv[])
{
    // test_f tests
    test_f(11, [](int p){_tprintf_s(_T(&quot;p: %d\n&quot;), p);});
    test_f(22, normal_func);
    test_f(33, normal_struct_func());
    
    // test_c tests
    // error C2976: 'test_c' : too few template arguments
    //test_c&lt;int&gt; tc1(11, [](int p){_tprintf_s(_T(&quot;p: %d\n&quot;), p);});
    //test_c&lt;int&gt; tc2(22, normal_func);
    //test_c&lt;int&gt; tc3(33, normal_struct_func());

    // error C2664: 'test_c&lt;T,Function&gt;::test_c(T,Function (__cdecl *))' : cannot convert parameter 2 from '`anonymous-namespace'::&lt;lambda1&gt;' to 'void (__cdecl *)(int)'
    // ...
    test_c&lt;int, void(int)&gt; tc1(11, [](int p){_tprintf_s(_T(&quot;p: %d\n&quot;), p);});
    test_c&lt;int, void(int)&gt; tc2(22, normal_func);
    test_c&lt;int, void(int)&gt; tc3(33, normal_struct_func());

    return 0;
}
</pre></p><p>&nbsp;</p><p>The 'test_f' works as it should but test_c class, which does the same basically, does not.</p><p>&nbsp;</p><p>Problem list:</p><p>1. How to make this compile. What am i missing / doing wrong.</p><p>2. Why do i have to type &quot;test_c&lt;int, void(int)&gt;&quot; instead of just &quot;test_c&lt;int&gt;&quot;, the compiler should be able to figure that out, it did that for &quot;test_f&quot;</p><p>&nbsp;</p><p>Thanks for your help.</p><p>&nbsp;</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/e9f0ab7ff08b4a0c95e19e92011c3a6a#e9f0ab7ff08b4a0c95e19e92011c3a6a</link>
		<pubDate>Tue, 22 Feb 2011 17:14:50 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/e9f0ab7ff08b4a0c95e19e92011c3a6a#e9f0ab7ff08b4a0c95e19e92011c3a6a</guid>
		<dc:creator>Mr Crash</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Mr Crash/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p><blockquote><div class="quoteText">Why do i have to type &quot;test_c&lt;int, void(int)&gt;&quot; instead of just &quot;test_c&lt;int&gt;&quot;</div></blockquote></p><p>&nbsp;</p><p>It works for test_f due to &quot;template argument deduction&quot;. This applies only to template functions, not to template classes.</p><p>&nbsp;</p><p><blockquote><div class="quoteText">How to make this compile. What am i missing / doing wrong.</div></blockquote></p><p>&nbsp;</p><p>Don't use void(int). That simply means a function that takes an int and returns nothing. normal_struct_func is not a function, it's an object that has a () operator aka function object, functor etc. You could use std::function (make sure you include &lt;functional&gt;):</p><p>&nbsp;</p><p><pre class="brush: cpp">test_c&lt;int, std::function&lt;void(int)&gt;&gt; tc3(33, normal_struct_func());</pre></p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/9b12130d261c40bbb2599e92012970c8#9b12130d261c40bbb2599e92012970c8</link>
		<pubDate>Tue, 22 Feb 2011 18:02:56 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/9b12130d261c40bbb2599e92012970c8#9b12130d261c40bbb2599e92012970c8</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>&gt; &quot;template argument deduction&quot;. This applies only to template functions, not to template classes.<br><br>I need to refresh my C&#43;&#43; knowledge.<br>I find it strange and kind of annoying that &quot;template argument deduction&quot; doesn't work for template classes.<br>I wonder what the reason was for this rule..<br><br>Yes, i did find that 'std::function' made it work but thought there must be a better way, lack of &quot;template argument deduction&quot; explains it.<br><br>What i'm trying to do is to create a template class that does stuff when it leaves scope:<br>Some unoptimized examples:<br>1:<br>scoped&lt;HMODULE&gt; dll(LoadLibrary(&quot;kernel32.dll&quot;), [](HMODULE h){FreeLibrary(h);})</p><p>2:<br>scoped&lt;int*&gt; ptr(new int, [](int *p){delete p;})<br>3:<br>scoped&lt;int[]&gt; ptr(new int[100], [](int *p){delete [] p;})<br>4:</p><p>scoped&lt;int&gt; ptr(1974, [](int p){ cout &lt;&lt; p &lt;&lt; endl;})</p><p><br>stuff like that.<br>I know of unique_ptr but i wanted to create a single template class that could do all of the above.<br><br>The class was all good with pointers and arrays but when i tried to make it do stuff like example 1 i hit a brick wall because i want it to work with all functor, see &quot;test_f tests&quot; for example.<br><br>It seems to be very tricky to get it to work.<br>One of the things i would like to minimize is the amount of typing required but the &quot;template argument deduction&quot; not for template classes rule, is in the way of that. auto keyword does n't help either.<br><br>Input, ideas, etc. are much appreciated.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/afe07e108d9e4d4c91929e92013a3505#afe07e108d9e4d4c91929e92013a3505</link>
		<pubDate>Tue, 22 Feb 2011 19:03:59 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/afe07e108d9e4d4c91929e92013a3505#afe07e108d9e4d4c91929e92013a3505</guid>
		<dc:creator>Mr Crash</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Mr Crash/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>In this particular case it seems to me that the simplest solution is to get rid of the Function template argument:</p><p><pre class="brush: cpp">template&lt;typename T&gt; 
class test_c {
public:    
    explicit test_c(T p, std::function&lt;void(T)&gt; func) 
        : m_p(p), m_Func(func) {
    }     
    
    ~test_c(void) {        
        m_Func(m_p);
    }
    
    T get(void) const {        
        return m_p;
    }

private:    
    T m_p;
    const std::function&lt;void(T)&gt; m_Func;
}; 
</pre></p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/c99b7c4a618f49cdbc939e9201457bfd#c99b7c4a618f49cdbc939e9201457bfd</link>
		<pubDate>Tue, 22 Feb 2011 19:45:03 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/c99b7c4a618f49cdbc939e9201457bfd#c99b7c4a618f49cdbc939e9201457bfd</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>@<a href="http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong#cafe07e108d9e4d4c91929e92013a3505">Mr Crash</a>: For your specific problem, I like Dexter's way better; however, since stuff like this pops up occassionally, here is another way:<pre class="brush: cpp">#include &lt;iostream&gt;
#include &lt;memory&gt;
#include &lt;functional&gt;

#include &lt;windows.h&gt;

using namespace std;

//=========================================================

/*
// Dexter's code:
template&lt;typename T&gt;  
class test_c { 
public:  
    explicit test_c(T p, std::function&lt;void(T)&gt; func)  
        : m_p(p), m_Func(func) { 
    }      
      
    ~test_c(void) {         
        m_Func(m_p); 
    } 
      
    T get(void) const {         
        return m_p; 
    } 
  
private:     
    T m_p; 
    const std::function&lt;void(T)&gt; m_Func; 
};
*/

// A dumbed-down version of the functors presented around
// page 110 of Modern C&#43;&#43; Design by Andrei Alexandrescu:
template &lt; typename RESULT_TYPE, typename PARAMETER_TYPE &gt;
class functor_base_t {
    public:
        virtual ~functor_base_t () {
            //
        }
        virtual RESULT_TYPE operator () ( PARAMETER_TYPE ) = 0;
};

template &lt; typename RESULT_TYPE, typename PARAMETER_TYPE, typename FUNCTOR &gt;
class functor_handler_t : public functor_base_t&lt; RESULT_TYPE, PARAMETER_TYPE &gt; {
    FUNCTOR m_functor;
    public:
        functor_handler_t ( FUNCTOR functor ) : m_functor( functor ) {
            //
        }
        virtual ~functor_handler_t () {
            //
        }
        virtual RESULT_TYPE operator () ( PARAMETER_TYPE parameter ) {
            return m_functor( parameter );
        }
};

template &lt; typename TYPE &gt;
class test_c {
    private:
        typedef void RESULT_TYPE;
        typedef TYPE PARAMETER_TYPE;

        PARAMETER_TYPE m_parameter;
        std::shared_ptr&lt; functor_base_t&lt; RESULT_TYPE, PARAMETER_TYPE &gt; &gt; m_functor_handler;
    public:
        template &lt; typename FUNCTOR &gt;
        explicit test_c ( PARAMETER_TYPE parameter, FUNCTOR functor ) :
            m_parameter( parameter ),
            m_functor_handler(
                new functor_handler_t&lt;
                    RESULT_TYPE,
                    PARAMETER_TYPE,
                    FUNCTOR
                &gt;( functor )
            )
        {
            //
        }
        ~test_c () {
            ( *m_functor_handler )( m_parameter );
        }
        PARAMETER_TYPE const &amp; get () const {
            return m_parameter;
        }
};

//=========================================================

inline void normal_func ( int p ) {
    cout &lt;&lt; &quot;p: &quot; &lt;&lt; p &lt;&lt; endl;
}

struct normal_struct_func {
    inline void operator() (int p) const {
        cout &lt;&lt; &quot;p: &quot; &lt;&lt; p &lt;&lt; endl;
    }
};

template&lt; typename T, typename Function &gt;
inline void test_f ( T p, Function func ) {
    func( p );
}

int main () {
    // test_f tests
    test_f( 11, [] ( int p ) { cout &lt;&lt; &quot;p: &quot; &lt;&lt; p &lt;&lt; endl; } );
    test_f( 22, normal_func );
    test_f( 33, normal_struct_func() );

    // test_c tests
    test_c&lt; int &gt; tc1( 11, [] ( int p ) { cout &lt;&lt; &quot;p: &quot; &lt;&lt; p &lt;&lt; endl; } );
    test_c&lt; int &gt; tc2( 22, normal_func );
    test_c&lt; int &gt; tc3( 33, normal_struct_func() );

    test_c&lt; int * &gt; tc4(
        new int,
        [] ( int * ptr ) {
            cout &lt;&lt; &quot;tc4: &quot; &lt;&lt; *ptr &lt;&lt; endl;
            delete ptr;
        }
    );
    *tc4.get() = 44;
    test_c&lt; int * &gt; tc5(
        new int[ 3 ],
        [] ( int * arr ) {
            int const * i = &amp;arr[ 0 ];
            int const * end = &amp;arr[ 3 ];
            cout &lt;&lt; &quot;tc5: { &quot;;
            if ( i != end ) {
                cout &lt;&lt; *i;
                for ( &#43;&#43;i ; i != end; &#43;&#43;i )
                    cout &lt;&lt; &quot;, &quot; &lt;&lt; *i;
            }
            cout &lt;&lt; &quot; }&quot; &lt;&lt; endl;
            delete[] arr;
        }
    );
    tc5.get()[ 0 ] = 55;
    tc5.get()[ 1 ] = 56;
    tc5.get()[ 2 ] = 57;

#define scoped test_c

    // 1:
    scoped&lt;HMODULE&gt; dll(LoadLibraryA(&quot;kernel32.dll&quot;), [](HMODULE h){FreeLibrary(h);});

    // 2:
    scoped&lt;int*&gt; ptr2(new int, [](int *p){delete p;});

    // 3:
    scoped&lt;int*&gt; ptr3(new int[100], [](int *p){delete[] p;});

    // 4:
    scoped&lt;int&gt; ptr4(1974, [](int p){ cout &lt;&lt; p &lt;&lt; endl;});

    return 0;
}
</pre>This works in VC&#43;&#43;2010 and MinGW's port of g&#43;&#43; 4.5.2.&nbsp; Here's the command line for MinGW's g&#43;&#43; 4.5.2:<pre class="brush: text">g&#43;&#43; -o main.exe main.cpp -std=c&#43;&#43;0x -march=native -O3 -Wall -Wextra -Werror</pre>This code produces the following output:<pre class="brush: text">p: 11
p: 22
p: 33
1974
tc5: { 55, 56, 57 }
tc4: 44
p: 33
p: 22
p: 11</pre></p><p>Hope This Also Helps,<br>Joshua Burkholder</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/63f29a96a5a2412db9de9e93000ed124#63f29a96a5a2412db9de9e93000ed124</link>
		<pubDate>Wed, 23 Feb 2011 00:53:56 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/63f29a96a5a2412db9de9e93000ed124#63f29a96a5a2412db9de9e93000ed124</guid>
		<dc:creator>Joshua Burkholder</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Burkholder/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p><div><p>Very interesting and helpful answers, thank you.</p><p>&nbsp;</p><p>I decided to see how many times i could shot myself in the foot.<br>Basically if i could make something that was lighter then the suggestions.</p><p>That is without std::shared_ptr and std::function.<br><br>And yet again i managed to get into a fight with the compiler<br><br><strong>This is sandbox code so expect strangeness and inconsistencies:</strong></p><p><strong><br></strong></p><p><pre class="brush: cpp">#include &lt;windows.h&gt; // UNREFERENCED_PARAMETER
#include &lt;assert.h&gt;
#include &lt;cstddef&gt;
#include &lt;utility&gt;

// A macro to disallow the copy constructor and operator= functions
// This should be used in the private: declarations for a class
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
    TypeName(const TypeName&amp;);               \
    void operator=(const TypeName&amp;)

// default_delete - scalar
template&lt;typename T&gt;
struct default_delete
{
    inline void operator()(T obj) const
    {
        // empty
    }
};

// default_delete - scalar pointer
template&lt;typename T&gt;
struct default_delete&lt;T*&gt;
{
    inline void operator()(T *ptr) const
    {
        delete ptr;
    }
};

// default_delete - array
template&lt;typename T&gt;
struct default_delete&lt;T[]&gt;
{
    inline void operator()(T *ptr) const
    {
        delete [] ptr;
    }
};

template&lt;typename T&gt;
struct default_empty {
    inline void operator()(T) const    {
        // empty
    }
};

// scalar version
template&lt;typename T, typename OnLeaveScopeFunc&gt;
class scoped {
public:
    explicit scoped(T p = 0, OnLeaveScopeFunc func = default_delete&lt;T&gt;) : m_obj(p), m_onLeaveScope(func) {}

    ~scoped(void)
    {
        m_onLeaveScope(m_obj);
    }

    // Common to both pointer and array
    void reset(T p = 0)
    {
        if (m_obj != p)
        {
            m_onLeaveScope(m_obj);
            m_obj = p;
        }
    }

    T get(void) const
    {
        return m_obj;
    }

    bool operator==(const T p) const
    {
        return m_obj == p;
    }

    bool operator!=(const T p) const
    {
        return !(m_obj == p);
    }

    // Move constructor and assignment
    // This will enable make_scoped to work
    scoped(scoped &amp;&amp;right)
    {
        m_obj = right.m_obj;
        m_onLeaveScope = right.m_onLeaveScope;
        right.m_obj = 0;
    }

    scoped &amp;operator=(scoped &amp;&amp;right)
    {
        scoped(std::move(right)).swap(*this); // Invoke move constructor and swap
        return *this;
    }

    void swap(scoped &amp;right)
    {
        std::swap(m_obj, right.m_obj);
        std::swap(m_onLeaveScope, right.m_onLeaveScope);
    }

    T release(void)
    {
        T temp = m_obj;
        m_obj = 0;
        return temp;
    }

private:
    T m_obj;
    OnLeaveScopeFunc m_onLeaveScope;

    DISALLOW_COPY_AND_ASSIGN(scoped);
};

// scalar pointer version
template&lt;typename T, typename OnLeaveScopeFunc&gt;
class scoped&lt;T*, OnLeaveScopeFunc&gt; {
public:
    typedef T element_type;

    explicit scoped(T *p = nullptr, OnLeaveScopeFunc func = default_delete&lt;T&gt;) : m_ptr(p), m_onLeaveScope(func), m_onLeaveScope_empty(default_empty&lt;T&gt;) {}

    ~scoped(void)
    {
        m_onLeaveScope(m_ptr);
    }

    // Common to both pointer and array
    void reset(T *p = nullptr)
    {
        if (m_ptr != p)
        {
            m_onLeaveScope(m_ptr);
            m_ptr = p;
        }
    }

    T *get(void) const
    {
        return m_ptr;
    }

    bool operator==(const T *p) const
    {
        return m_ptr == p;
    }

    bool operator!=(const T *p) const
    {
        return !(m_ptr == p);
    }

    // Move constructor and assignment
    // This will enable make_scoped to work
    scoped(scoped &amp;&amp;right)
    {
        m_ptr = right.m_ptr;
        m_onLeaveScope = right.m_onLeaveScope;
        right.m_ptr = nullptr;
        right.m_onLeaveScope = m_onLeaveScope_empty;
    }

    scoped &amp;operator=(scoped &amp;&amp;right)
    {
        scoped(std::move(right)).swap(*this); // Invoke move constructor and swap
        return *this;
    }

    void swap(scoped &amp;right)
    {
        std::swap(m_ptr, right.m_ptr);
        std::swap(m_onLeaveScope, right.m_onLeaveScope);
    }

    T *release(void)
    {
        T *temp = m_ptr;
        m_ptr = nullptr;
        return temp;
    }

    // Scalar pointer version
    T &amp;operator*(void) const
    {
        assert(m_ptr != nullptr);
        return *m_ptr;
    }

    T *operator-&gt;(void) const
    {
        assert(m_ptr != nullptr);
        return m_ptr;
    }

private:
    T *m_ptr;
    OnLeaveScopeFunc m_onLeaveScope;
    OnLeaveScopeFunc m_onLeaveScope_empty;

    DISALLOW_COPY_AND_ASSIGN(scoped);
};

// array version
// FIXME: Any way to prevent missing [] from compiling ? Ex. scoped&lt;int&gt; bla(new int[])
// or to detect it based on how it is used further down the code ?
template&lt;typename T, typename OnLeaveScopeFunc&gt;
class scoped&lt;T[], OnLeaveScopeFunc&gt; {
public:
    typedef T element_type;

    explicit scoped(T *p = nullptr, OnLeaveScopeFunc func = default_delete&lt;T&gt;) : m_ptr(p), m_onLeaveScope(func) {}

    ~scoped(void)
    {
        m_onLeaveScope(m_ptr);
    }

    // Common to both pointer and array
    void reset(T *p = nullptr)
    {
        if (m_ptr != p)
        {
            m_onLeaveScope(m_ptr);
            m_ptr = p;
        }
    }

    T *get(void) const
    {
        return m_ptr;
    }

    bool operator==(const T *p) const
    {
        return m_ptr == p;
    }

    bool operator!=(const T *p) const
    {
        return !(m_ptr == p);
    }

    // Move constructor and assignment
    // This will enable make_scoped to work
    scoped(scoped &amp;&amp;right)
    {
        m_ptr = right.m_ptr;
        m_onLeaveScope = right.m_onLeaveScope;
        right.m_ptr = nullptr;
    }

    scoped &amp;operator=(scoped &amp;&amp;right)
    {
        scoped(std::move(right)).swap(*this); // Invoke move constructor and swap
        return *this;
    }

    void swap(scoped &amp;right)
    {
        std::swap(m_ptr, right.m_ptr);
        std::swap(m_onLeaveScope, right.m_onLeaveScope);
    }

    T *release(void)
    {
        T *temp = m_ptr;
        m_ptr = nullptr;
        return temp;
    }

    // Array version
    // Should we use uintptr_t instead of ptrdiff_t ?
    T &amp;operator[](std::ptrdiff_t i) const
    {
        assert(i &gt;= 0);
        assert(m_ptr != nullptr);
        return m_ptr[i];
    }

private:
    T *m_ptr;
    OnLeaveScopeFunc m_onLeaveScope;

    DISALLOW_COPY_AND_ASSIGN(scoped);
};

// template argument deduction goodness
template &lt;typename T, typename F&gt;
inline scoped&lt;T, F&gt; make_scoped(T t, F f)
{
    return std::move(scoped&lt;T, F&gt;(t, f));
}

//----------------------------------------------------

inline void normal_f(int p) {
    _tprintf_s(_T(&quot;normal_f p: %d\n&quot;), p);
}

struct normal_struct_f {
    inline void operator()(int p) const {
        _tprintf_s(_T(&quot;normal_struct_f p: %d\n&quot;), p);
    }
};

inline void normal_fp(int *p) {
    _tprintf_s(_T(&quot;normal_fp *p: %d\n&quot;), *p);
}

struct normal_struct_fp {
    inline void operator()(int *p) const {
        _tprintf_s(_T(&quot;normal_struct_fp *p: %d\n&quot;), *p);
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    UNREFERENCED_PARAMETER(argc);
    UNREFERENCED_PARAMETER(argv);

    // 2010-02-24
    auto f = [](int p) {
        _tprintf_s(_T(&quot;f p: %d\n&quot;), p);
    };

    auto fp = [](int *p) {
        _tprintf_s(_T(&quot;fp *p: %d\n&quot;), *p);
    };

    {
        auto t1 = make_scoped(1975, f);
        auto t2 = make_scoped(1975, normal_f);
        auto t3 = make_scoped(1975, normal_struct_f());
    }
    _tprintf_s(_T(&quot;\n&quot;)); // For output readability
    {
        auto t1 = make_scoped(new int(1975), fp);
        auto t2 = make_scoped(new int(1975), normal_fp);
        auto t3 = make_scoped(new int(1975), normal_struct_fp());
    }
    _tprintf_s(_T(&quot;\n&quot;)); // For output readability
    return 0;
}
</pre></p><p><br>Ok, so the problem i'm having:<br>In &lt;T*&gt; version of 'scoped(scoped &amp;&amp;right)' (that's the class i've used as test subject)<br>How to make 'right.m_onLeaveScope' accept the new empty function ?</p><p><br>The compiler complains:<br>&quot;error C2275: 'default_empty&lt;T&gt;' : illegal use of this type as an expression&quot;<br>due to 'm_onLeaveScope_empty(default_empty&lt;T&gt;)' on line<br>explicit scoped(T *p = nullptr, OnLeaveScopeFunc func = default_delete&lt;T&gt;) : m_ptr(p), m_onLeaveScope(func), m_onLeaveScope_empty(default_empty&lt;T&gt;) {}<br><br>I can't see why i doesn't want to work.</p><p>&quot;OnLeaveScopeFunc func = default_delete&lt;T&gt;&quot;</p><p>Worked fine so why doesn't that want to work ?</p><p>I've also tried:</p><p>&quot;OnLeaveScopeFunc func_empty = default_empty&lt;T&gt;&quot;</p><p>'m_onLeaveScope_empty(func_empty)'</p><p>&nbsp;</p><p>explicit scoped(T *p = nullptr, OnLeaveScopeFunc func = default_delete&lt;T&gt;, OnLeaveScopeFunc func_empty = default_empty&lt;T&gt;) : m_ptr(p), m_onLeaveScope(func), m_onLeaveScope_empty(func_empty)<span class="Apple-converted-space">&nbsp;</span>{}</p></div></p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/7af4e67216424ea787bc9e940029c6e2#7af4e67216424ea787bc9e940029c6e2</link>
		<pubDate>Thu, 24 Feb 2011 02:32:06 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/7af4e67216424ea787bc9e940029c6e2#7af4e67216424ea787bc9e940029c6e2</guid>
		<dc:creator>Mr Crash</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Mr Crash/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>You're trying to assign a type name (default_delete&lt;T&gt;) to a variable (m_onLeaveScope_empty), you forgot a ():</p><p><pre class="brush: cpp">m_onLeaveScope_empty(default_empty&lt;T&gt;()) </pre></p><p>It worked in the case of the func argument because you never use the default value in your tests. As soon as you&nbsp;write something like scoped&lt;int, decltype(f)&gt; f23(23); you'll get the same error.&nbsp;</p><p>&nbsp;</p><p>Anyway, it still won't work. Once you fix this you'll get another error: '`anonymous-namespace'::&lt;lambda1&gt;::(const `anonymous-namespace'::&lt;lambda1&gt; &amp;)' : cannot convert parameter 1 from 'default_empty&lt;T&gt;' to 'const `anonymous-namespace'::&lt;lambda1&gt; &amp;'</p><p>&nbsp;</p><p>Again, you're mixing up things that look like functions but they have different types. In the particular case that triggered the above error your deleter is a&nbsp;lambda and the default_empty is a function object. They're not compatible.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/a5a996391e9b47fbb2f99e940087263a#a5a996391e9b47fbb2f99e940087263a</link>
		<pubDate>Thu, 24 Feb 2011 08:12:03 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/a5a996391e9b47fbb2f99e940087263a#a5a996391e9b47fbb2f99e940087263a</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>&gt; Anyway, it still won't work. Once you fix this you'll get another error: '`anonymous-namespace'::&lt;lambda1&gt;::(const `anonymous-namespace'::&lt;lambda1&gt; &amp;)' : cannot convert parameter 1 from 'default_empty&lt;T&gt;' to 'const `anonymous-namespace'::&lt;lambda1&gt; &amp;'</p><p>&nbsp;</p><p>I knew i forgot to mention something, it was 3 am when i wrote that, sorry about that.</p><p>I&nbsp;did get that error too, which made me confused and got me thinking i've forgot some c&#43;&#43; rule again.</p><p>&nbsp;</p><p>&gt; Again, you're mixing up things that look like functions but they have different types.</p><p>&nbsp;</p><p>Yes, can you explain what i'm missing, please ?</p><p>&nbsp;</p><p>How would i make it work or is that not a possibility without&nbsp;std::function ?</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/c09d067310aa4683b7089e9400e62880#c09d067310aa4683b7089e9400e62880</link>
		<pubDate>Thu, 24 Feb 2011 13:57:58 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/c09d067310aa4683b7089e9400e62880#c09d067310aa4683b7089e9400e62880</guid>
		<dc:creator>Mr Crash</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Mr Crash/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p><blockquote><div class="quoteText">Yes, can you explain what i'm missing, please ?</div></blockquote></p><p>Let's take the case of make_scoped(new int(1975), normal_struct_fp()). This creates a scoped&lt;int, normal_struct_fp&gt; where m_onLeaveScope_empty is of type normal_struct_fp. And the scoped constructor is trying to assign an object of type default_empty&lt;int&gt; to a variable of type normal_struct_fp. That won't work, just because&nbsp;2 types have an () operator with the same signature it doesn't mean that you can convert from one type to another.</p><p><blockquote><div class="quoteText">How would i make it work or is that not a possibility without std::function?</div></blockquote></p><p>Well, it seems to me that the only purpose of m_onLeaveScope_empty is to support the move constructor. You could leave the old m_onLeaveScope alone and add a null check in the scoped destructor so the old m_onLeaveScope doesn't get called.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/9f09ac030f674973b80b9e94010cbd5c#9f09ac030f674973b80b9e94010cbd5c</link>
		<pubDate>Thu, 24 Feb 2011 16:18:27 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/9f09ac030f674973b80b9e94010cbd5c#9f09ac030f674973b80b9e94010cbd5c</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p><blockquote><div class="quoteText"></p><p><a class="permalink" title="Post Permalink" href="http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/9f09ac030f674973b80b9e94010cbd5c">53 minutes&nbsp;ago</a>, <a href="http://channel9.msdn.com/Niners/Dexter">Dexter</a> wrote</p><p>Well, it seems to me that the only purpose of m_onLeaveScope_empty is to support the move constructor. You could leave the old m_onLeaveScope alone and add a null check in the scoped destructor so the old m_onLeaveScope doesn't get called.</p><p></div></blockquote></p><p>Yes, I've thought of that,&nbsp;it would work for the pointer version, but not for the non pointer version.</p><p>We can't really test for m_obj != 0 since 0 might be a totally valid value.</p><p>&nbsp;</p><p>The possible solution would be to add a bool that gets set to true (is_empty = true) when the data is moved.</p><p>This, i'm guessing, would also be faster then calling an empty function.</p><p>&nbsp;</p><p>But i'm not sure which way to go. I want it to be correct and as performance as possible so i'm still wondering if there is some kind of&nbsp; template magic to sole this.</p><p>&nbsp;</p><p>But i'm leaning to the bool solution or perhaps i should use std::function...</p><p>Am i overthinking it ?</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/418a55b20b63406bb1239e94011e8d0b#418a55b20b63406bb1239e94011e8d0b</link>
		<pubDate>Thu, 24 Feb 2011 17:23:18 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/418a55b20b63406bb1239e94011e8d0b#418a55b20b63406bb1239e94011e8d0b</guid>
		<dc:creator>Mr Crash</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Mr Crash/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>Yep, the non pointer version is an issue, didn't look at it as it didn't have compile errors <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-1.gif?v=c9' alt='Smiley' /></p><p>Performance wise I'd say that the bool variant is the fastest. std::function is cleaner but it has&nbsp;larger time &amp; space overhead. But you know what they say, premature optimization is the root of all evil. Things like CreateFile or LoadLibrary will take far more time to complete than the few instructions that are need by make_scope/scoped.</p><p>If you really want it to be fast then the first thing you'll want to do is to get rid of make_scoped, it's not &quot;free&quot;.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/1913bb8a1d3242a2aa3a9e940127a594#1913bb8a1d3242a2aa3a9e940127a594</link>
		<pubDate>Thu, 24 Feb 2011 17:56:25 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/1913bb8a1d3242a2aa3a9e940127a594#1913bb8a1d3242a2aa3a9e940127a594</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>Interesting topic.<br>But the code examples above will generate a lot of overhead for the small tasks it seems to be for. For example using it as a scoped_array.<br>Best would be if you could make the optimizer inline , etc..<br>Make the compiler do the heavy work for us.</p><p>Like this little&nbsp;code example:<br><pre class="brush: cpp">inline void normal_function(int v) {
    cout &lt;&lt; &quot;normal_function: &quot; &lt;&lt; v &lt;&lt; endl;
}

template &lt;typename Function&gt;
class sop {
    Function f_;
public:
    sop(Function fun) : f_(fun) {}
    ~sop() {
        f_();
    }
};

void t_sop()
{
    auto l = [](){
        normal_function(123);
    };
    sop&lt;decltype(l)&gt; s(l);
}

int main()
{
    t_sop();
    return 0;
}</pre>&nbsp;</p><p>&nbsp;</p><p>Generated code in win32 release mode:<br><pre class="brush: text">    t_sop();
00291000  mov         eax,dword ptr [__imp_std::endl (292044h)]  
00291005  mov         ecx,dword ptr [__imp_std::cout (292068h)]  
0029100B  push        eax  
0029100C  push        7Bh  
0029100E  push        offset string &quot;normal_function: &quot; (292114h)  
00291013  push        ecx  
00291014  call        std::operator&lt;&lt;&lt;std::char_traits&lt;char&gt; &gt; (2910F0h)  
00291019  add         esp,8  
0029101C  mov         ecx,eax  
0029101E  call        dword ptr [__imp_std::basic_ostream&lt;char,std::char_traits&lt;char&gt; &gt;::operator&lt;&lt; (292050h)]  
00291024  mov         ecx,eax  
00291026  call        dword ptr [__imp_std::basic_ostream&lt;char,std::char_traits&lt;char&gt; &gt;::operator&lt;&lt; (29204Ch)]  
</pre></p><p><br>Shouldn't you be able to&nbsp;achieve&nbsp;this with the power of c&#43;&#43;0x but without this big overhead ?</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/e966f45825ac42639c789f2c001fb8e1#e966f45825ac42639c789f2c001fb8e1</link>
		<pubDate>Tue, 26 Jul 2011 01:55:29 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/e966f45825ac42639c789f2c001fb8e1#e966f45825ac42639c789f2c001fb8e1</guid>
		<dc:creator>Jonas_No</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Jonas_No/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">Best would be if you could make the optimizer inline , etc..</div></blockquote><p></p><p>I don't understand... that's exactly what the compiler did. The code in main is the code from normal_function, 3 calls to the stream insertion operator and nothing else.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/d701fa097b8c4289b5519f2c0059162a#d701fa097b8c4289b5519f2c0059162a</link>
		<pubDate>Tue, 26 Jul 2011 05:24:21 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/d701fa097b8c4289b5519f2c0059162a#d701fa097b8c4289b5519f2c0059162a</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>@<a href="/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong#cd701fa097b8c4289b5519f2c0059162a">Dexter</a>: The code in my post was just an example of compiler optimization. How a good template class should work.</p><p>Both your code and&nbsp;Burkholder's code have a big overhead.<br>Is there a way to minimize the overhead like in my example ?&nbsp;</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/b17d6d3c8717452a9ee99f2c00ed7f0a#b17d6d3c8717452a9ee99f2c00ed7f0a</link>
		<pubDate>Tue, 26 Jul 2011 14:24:41 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/b17d6d3c8717452a9ee99f2c00ed7f0a#b17d6d3c8717452a9ee99f2c00ed7f0a</guid>
		<dc:creator>Jonas_No</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Jonas_No/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>OK, I understand now. I don't know of a way to remove this overhead, std::function is horrible complex and the compiler has little chance to optimize it.</p><p>It seems to me that if you don't provide the function type in the template argument list (like in the original code or in your code) then the only alternative is to use std::function or something that does something similar: wraps a callable object. And this wrapping is complex because there are different types of callable objects: pointer to function, pointer to member, functors.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/481da36fd4f443e29dcf9f2c0130d26c#481da36fd4f443e29dcf9f2c0130d26c</link>
		<pubDate>Tue, 26 Jul 2011 18:29:49 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/481da36fd4f443e29dcf9f2c0130d26c#481da36fd4f443e29dcf9f2c0130d26c</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>I posted this link on last Advanced STL show: <a href="http://blog.tomaka17.com/2011/01/some-interesting-usages-of-stdfunction/">http&#58;&#47;&#47;blog.tomaka17.com&#47;2011&#47;01&#47;some-interesting-usages-of-stdfunction&#47;</a></p><p>here an except of the code (how to register a function to call)</p><p><pre class="brush: cpp">template &lt;typename TEvent, typename TListener &gt;
void _registerListener (const TListener&amp; function) {
static_assert(!std::is_array&lt;TEvent&gt;::value, &quot;You cannot register an array as an event type&quot;);
static_assert(std::is_convertible&lt;TListener,std::function&lt;void(TEvent&amp;)&gt;&gt;::value, &quot;Unvalid callback for this event type&quot;);
_listeners.insert(std::make_pair(&amp;typeid(std::decay&lt;TEvent&gt;::type), [=](void* ev) { function(*static_cast&lt;TEvent*&gt;(ev)); }));
}</pre></p><p><em>is_convertible</em> and <em>decay</em> seens what you are looking for deal with functors, function and lambdas. There are other interesting stuff on the blog. (I think I see an example with <em>std::bind</em> for additional &quot;magic&quot;call somewhere, I'll try re-digg it)</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/3d16e1be4afb42f7ac979f2e01229720#3d16e1be4afb42f7ac979f2e01229720</link>
		<pubDate>Thu, 28 Jul 2011 17:38:00 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/3d16e1be4afb42f7ac979f2e01229720#3d16e1be4afb42f7ac979f2e01229720</guid>
		<dc:creator>new2STL</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/new2STL/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>Here's how STL would do it:<br><a href="http://channel9.msdn.com/Shows/Going&#43;Deep/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-6-of-n#c634477472460000000">http://channel9.msdn.com/Shows/Going&#43;Deep/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-6-of-n#c634477472460000000</a></p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/dcb8826e776e422db9229f3300dc3489#dcb8826e776e422db9229f3300dc3489</link>
		<pubDate>Tue, 02 Aug 2011 13:21:44 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/dcb8826e776e422db9229f3300dc3489#dcb8826e776e422db9229f3300dc3489</guid>
		<dc:creator>Jonas_No</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Jonas_No/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>STL's&nbsp;ScopeWarden class looks promising.<br>I'm a bit disappointed that c&#43;&#43; is limited in the area of functor and lambda storing.<br>I wonder why they have not done anything about it.<br>Is it not common enough or do people just bite the bullet and take the dynamic allocation overhead and hope the cpu's get faster next year ?&nbsp;</p><p>Somebody from the c&#43;&#43; group should really comment on this.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/ffe28a5cb29742a6ba7c9f3501345873#ffe28a5cb29742a6ba7c9f3501345873</link>
		<pubDate>Thu, 04 Aug 2011 18:42:39 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/ffe28a5cb29742a6ba7c9f3501345873#ffe28a5cb29742a6ba7c9f3501345873</guid>
		<dc:creator>Mr Crash</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Mr Crash/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>What is &quot;limited&quot;?</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/e808a4a2c37d40d08f449f36000d76ef#e808a4a2c37d40d08f449f36000d76ef</link>
		<pubDate>Fri, 05 Aug 2011 00:49:01 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/e808a4a2c37d40d08f449f36000d76ef#e808a4a2c37d40d08f449f36000d76ef</guid>
		<dc:creator>STL</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/STL/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>@<a href="/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong#ce808a4a2c37d40d08f449f36000d76ef">STL</a>:Limited as in not being able to store functors and lambdas without overhead or workaround (you called it new trickery)&nbsp;&quot;You need placement new trickery for arbitrary functors.&quot;&nbsp;</p><p>Was that what you were asking ? You're question is vague, can you&nbsp;rephrase&nbsp;the question ?</p><p>I absolutely love c&#43;&#43; but it do have it's&nbsp;flaws like this one which i think is one of those things that have been overlooked.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/efbcb7cdc305415a83269f37000c2d1c#efbcb7cdc305415a83269f37000c2d1c</link>
		<pubDate>Sat, 06 Aug 2011 00:44:19 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/efbcb7cdc305415a83269f37000c2d1c#efbcb7cdc305415a83269f37000c2d1c</guid>
		<dc:creator>Mr Crash</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Mr Crash/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>Well well, i've found something that looks good and doesn't use a marco, sad part, vs2010 cant handle it <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-6.gif?v=c9' alt='Sad' /></p><p><a href="http://pizer.wordpress.com/2008/11/22/scope-guards-revisited-c0x-style/#more-188">http://pizer.wordpress.com/2008/11/22/scope-guards-revisited-c0x-style/#more-188<br><br></a>Arr where's that vs2011 beta ?!</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/23207486318545099b409f370014775c#23207486318545099b409f370014775c</link>
		<pubDate>Sat, 06 Aug 2011 01:14:30 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/23207486318545099b409f370014775c#23207486318545099b409f370014775c</guid>
		<dc:creator>Mr Crash</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Mr Crash/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>&gt; Limited as in not being able to store functors and lambdas without overhead or workaround</p><p>std::function encapsulates that machinery. It inherently has nonzero cost, and inherently must throw exceptions for&nbsp;arbitrarily large functors.</p><p>There is no flaw to solve here.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/1ac46330fbd8478f8d5a9f3800100149#1ac46330fbd8478f8d5a9f3800100149</link>
		<pubDate>Sun, 07 Aug 2011 00:58:16 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/1ac46330fbd8478f8d5a9f3800100149#1ac46330fbd8478f8d5a9f3800100149</guid>
		<dc:creator>STL</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/STL/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>@<a href="/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong#c1ac46330fbd8478f8d5a9f3800100149">STL</a>: std:function still have overhead which makes it less usable for doing small things. Like using in a scope guard class or special callbacks, etc..</p><p>Take this code and compile it in release mode / win32 and look at it in debugging.</p><p><pre class="brush: cpp">#include &lt;iostream&gt;
#include &lt;functional&gt;

using namespace std;

inline void func() {
    cout &lt;&lt; &quot;func&quot; &lt;&lt; endl;
}

struct s_func {
    inline void operator()() {
        cout &lt;&lt; &quot;s_func&quot; &lt;&lt; endl;
    }
};

template &lt;typename Functor&gt;
void test_template(Functor f) {
    f();
}

void test_function(function&lt;void()&gt; f) {
    f();
}

int main()
{
    test_template(func);
    test_function(func);

    test_template(s_func());
    test_function(s_func());
}
</pre></p><p>test_template(func):</p><p><pre class="brush: text">    test_template(func);
00301095  mov         eax,dword ptr [__imp_std::endl (302044h)]  
0030109A  mov         ecx,dword ptr [__imp_std::cout (302068h)]  
003010A0  push        eax  
003010A1  push        offset string &quot;func&quot; (30214Ch)  
003010A6  push        ecx  
003010A7  call        std::operator&lt;&lt;&lt;std::char_traits&lt;char&gt; &gt; (301240h)  
003010AC  add         esp,8  
003010AF  mov         ecx,eax  
003010B1  call        dword ptr [__imp_std::basic_ostream&lt;char,std::char_traits&lt;char&gt; &gt;::operator&lt;&lt; (30204Ch)]  </pre></p><p>test_function(func); : (called functions not included)</p><p><pre class="brush: text">    test_function(func);
003010B7  mov         edx,offset func (301040h)  
003010BC  test        edx,edx  
003010BE  jne         main&#43;54h (3010C4h)  
003010C0  xor         ecx,ecx  
003010C2  jmp         main&#43;65h (3010D5h)  
003010C4  mov         dword ptr [ebp-24h],offset std::tr1::_Impl_no_alloc0&lt;std::tr1::_Callable_fun&lt;void (__cdecl*const)(void),0&gt;,void&gt;::`vftable' (302188h)  
003010CB  mov         dword ptr [ebp-20h],offset func (301040h)  
003010D2  lea         ecx,[ebp-24h]  
003010D5  mov         dword ptr [ebp-14h],ecx  
003010D8  mov         dword ptr [ebp-4],0  
003010DF  test        ecx,ecx  
003010E1  jne         $LN163 (3010E9h)  
003010E3  call        dword ptr [__imp_std::tr1::_Xfunc (302064h)]  
$LN163:
003010E9  mov         eax,dword ptr [ecx]  
003010EB  mov         edx,dword ptr [eax&#43;4]  
003010EE  call        edx  
003010F0  mov         dword ptr [ebp-4],0FFFFFFFFh  
003010F7  mov         ecx,dword ptr [ebp-14h]  
003010FA  test        ecx,ecx  
003010FC  je          $LN163&#43;28h (301111h)  
003010FE  mov         eax,dword ptr [ecx]  
00301100  mov         eax,dword ptr [eax&#43;0Ch]  
00301103  lea         edx,[ebp-24h]  
00301106  cmp         ecx,edx  
00301108  setne       dl  
0030110B  movzx       edx,dl  
0030110E  push        edx  
0030110F  call        eax  </pre></p><p>function 'test_template' is a clear winner. Now the sad part is that you can't do this in a class without overhead because you have to store the function object which isn't really supported so you have to resort to tricks which have overhead.</p><p><pre class="brush: cpp">// class version equivalent to test_template
// This should have been supported but nope
class scope_guard {
    auto f;
public:
    template &lt;typename Functor&gt;
    scope_guard(Functor f_obj) : f(f_obj) { // &lt;- ???
    }
    ~scope_guard() {
        f();
    }
</pre></p><p>function &quot;test_template&quot; works because you don't have to store the function object</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/757baae844f543cb8fcf9f380029be35#757baae844f543cb8fcf9f380029be35</link>
		<pubDate>Sun, 07 Aug 2011 02:31:58 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/757baae844f543cb8fcf9f380029be35#757baae844f543cb8fcf9f380029be35</guid>
		<dc:creator>Mr Crash</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Mr Crash/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>How about this:</p><p><pre class="brush: cpp">template&lt;typename Functor&gt;
class scope_guard
{
public:
    explicit scope_guard(Functor &amp;f) throw()
        : _f(std::addressof(f)) 
    { 
    }

    ~scope_guard() throw()
    {
        if( _f != nullptr )
        {
            try
            {
                (*_f)();
            }
            catch(...)
            {
                std::terminate();
            }
        }
    }

    scope_guard(scope_guard&lt;Functor&gt; &amp;&amp;other) throw()
        : _f(other._f)
    {
        other._f = nullptr;
    }

    scope_guard&lt;Functor&gt;&amp; operator=(scope_guard&lt;Functor&gt; &amp;&amp;other) throw()
    {
        if( &amp;other != this )
        {
            _f = other._f;
            other._f == nullptr;
        }
        return *this;
    }
private:
    scope_guard(const scope_guard&lt;Functor&gt;&amp;);
    scope_guard&lt;Functor&gt;&amp; operator=(const scope_guard&lt;Functor&gt;&amp;);

    Functor *_f;
};

template&lt;typename Functor&gt;
inline scope_guard&lt;Functor&gt; create_scope_guard(Functor &amp;f) throw()
{
    return scope_guard&lt;Functor&gt;(f);
}</pre></p><p>This makes the scope_guard non-copyable but movable, so it can be returned by value without triggering the action of desctructor more than once. This allows the create_scope_guard function to do the template parameter deduction for you.</p><p>Use as:</p><p><pre class="brush: cpp">auto guard = create_scope_guard([]() { cout &lt;&lt; &quot;foo&quot; &lt;&lt; endl; });</pre></p><p>It's not entirely your syntax, but it's not too bad, I think. <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-1.gif?v=c9' alt='Smiley' /></p><p>It's essentially like STL's class but using a move constructor and a function in place of the macro.</p><p>Note that due to the behavior of copy elision in VC&#43;&#43; the move constructor isn't strictly necessary. VC&#43;&#43; lets you return a non-copyable object by value if it can perform copy elision. By contrast, g&#43;&#43; will not allow you to return an object by value without a public copy or move constructor, even if neither would be called due to copy elision.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/bc876b2596134b2abd9b9f39008e62c4#bc876b2596134b2abd9b9f39008e62c4</link>
		<pubDate>Mon, 08 Aug 2011 08:38:24 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/bc876b2596134b2abd9b9f39008e62c4#bc876b2596134b2abd9b9f39008e62c4</guid>
		<dc:creator>Sven Groot</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Sven Groot/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>@<a href="/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong#cbc876b2596134b2abd9b9f39008e62c4">Sven Groot</a>: Thanks for joining the discussion.</p><p>I remember doing something similar. The problem was the same as in your suggestion.</p><p>It doesn't work for plain functions, in your case it because of &quot;std::addressof&quot;.</p><p>I remember trying to use&nbsp;some template magic, traits, is_function&lt;..&gt;, conditional, enable_if, &nbsp;to make the compiler choose between two version but i failed miserably.</p><p>The compiler just didn't want to play ball. Either it's due to bugs in the compiler or me missing some small detail.</p><p>I just could figure out why it didn't work. Not even g&#43;&#43;'s more helpful error messages helped me.</p><p>About 'Mr Crash' auto in class&nbsp;pseudo&nbsp;code:&nbsp;Would have been sweet if auto worked in classes like that. It would have reduced the pain of some template programming though it would surely cause other problems.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/bcc2af614b264af39f239f3900c9fc6a#bcc2af614b264af39f239f3900c9fc6a</link>
		<pubDate>Mon, 08 Aug 2011 12:15:24 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/bcc2af614b264af39f239f3900c9fc6a#bcc2af614b264af39f239f3900c9fc6a</guid>
		<dc:creator>Jonas_No</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Jonas_No/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p><br>Please tell me how bad this code is:</p><p>&nbsp;</p><p><pre class="brush: cpp">template&lt;typename Functor&gt;
class scope_guard {
public:
    explicit scope_guard(Functor &amp;f, false_type) throw()
        : _f(addressof(f))
    {}

    explicit scope_guard(Functor &amp;f, true_type) throw()
        : _f(f)
    {}

    ~scope_guard() throw()

    {
        if( _f != nullptr )
        {
            try
            {
                (*_f)();
            }
            catch(...)
            {
                std::terminate();
            }
        }
    }

    scope_guard(scope_guard&lt;Functor&gt; &amp;&amp;other) throw()
        : _f(other._f)
    {
        other._f = nullptr;
    }

    scope_guard&lt;Functor&gt;&amp; operator=(scope_guard&lt;Functor&gt; &amp;&amp;other) throw()
    {
        if( &amp;other != this )
        {
            _f = other._f;
            other._f == nullptr;
        }
        return *this;
    }
private:
    scope_guard(const scope_guard&lt;Functor&gt;&amp;);
    scope_guard&lt;Functor&gt;&amp; operator=(const scope_guard&lt;Functor&gt;&amp;);

    Functor *_f;
};

template&lt;typename Functor&gt;
inline scope_guard&lt;Functor&gt; create_scope_guard(Functor &amp;f) throw()
{
    return scope_guard&lt;Functor&gt;(f, typename is_function&lt;Functor&gt;::type());
}

inline void noarg_func( void ) {
    cout &lt;&lt; &quot;noarg_func&quot; &lt;&lt; endl;
}
struct noarg_functor {
    inline void operator()( void ) const {
        cout &lt;&lt; &quot;noarg_functor&quot; &lt;&lt; endl;
    }
};

void test() {
    auto guard1 = create_scope_guard([](){cout &lt;&lt; &quot;noarg_lambda&quot; &lt;&lt; endl;});
    auto guard2 = create_scope_guard(noarg_functor());
    auto guard3 = create_scope_guard(noarg_func); // &lt;- Doesn't get inlined
}

int main()
{
    test();
}
</pre></p><p>&nbsp;</p><p>Only problem is that this class use of &quot;&nbsp;C4239: nonstandard extension used ...&quot;<br>which is very bad&nbsp;</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/e482f93441c34e348f2d9f3900d5e2ea#e482f93441c34e348f2d9f3900d5e2ea</link>
		<pubDate>Mon, 08 Aug 2011 12:58:44 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/e482f93441c34e348f2d9f3900d5e2ea#e482f93441c34e348f2d9f3900d5e2ea</guid>
		<dc:creator>Jonas_No</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Jonas_No/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>@<a href="/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong#cbcc2af614b264af39f239f3900c9fc6a">Jonas_No</a>:</p><p>I came up with this, which uses an enable_if selected template function to pick how to get the address of the function.</p><p><pre class="brush: cpp">template&lt;typename Functor&gt;
class scope_guard
{
public:
    scope_guard(Functor &amp;f) throw()
        : _f(addressof(f)) 
    {
    }

    ~scope_guard() throw()
    {
        if( _f != nullptr )
        {
            try
            {
                (*_f)();
            }
            catch(...)
            {
                std::terminate();
            }
        }
    }

    scope_guard(scope_guard&lt;Functor&gt; &amp;&amp;other) throw()
        : _f(other._f)
    {
        other._f = nullptr;
    }

    scope_guard&lt;Functor&gt;&amp; operator=(scope_guard&lt;Functor&gt; &amp;&amp;other) throw()
    {
        if( &amp;other != this )
        {
            _f = other._f;
            other._f == nullptr;
        }
        return *this;
    }
private:
    template&lt;typename T&gt;
    static typename std::enable_if&lt;std::is_function&lt;T&gt;::value, T*&gt;::type addressof(T &amp;value)
    {
        return &amp;value;
    }

    template&lt;typename T&gt;
    static typename std::enable_if&lt;!std::is_function&lt;T&gt;::value, T*&gt;::type addressof(T &amp;value)
    {
        return std::addressof(value);
    }

    scope_guard(const scope_guard&lt;Functor&gt;&amp;);
    scope_guard&lt;Functor&gt;&amp; operator=(const scope_guard&lt;Functor&gt;&amp;);

    Functor *_f;
};

template&lt;typename Functor&gt;
inline scope_guard&lt;Functor&gt; create_scope_guard(Functor &amp;f) throw()
{
    return scope_guard&lt;Functor&gt;(f);
}</pre></p><p>It works in VC&#43;&#43;, but g&#43;&#43; is being more difficult, particularly because it doesn't support std::addressof, but also because it can't bind a lambda to a non-const reference parameter.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/0026595c0c0a4c7bafea9f3900de251f#0026595c0c0a4c7bafea9f3900de251f</link>
		<pubDate>Mon, 08 Aug 2011 13:28:48 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/0026595c0c0a4c7bafea9f3900de251f#0026595c0c0a4c7bafea9f3900de251f</guid>
		<dc:creator>Sven Groot</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Sven Groot/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>This makes the warnings go away in vs2010 but g&#43;&#43; is smarter then vs2010 it, lol, and gives an error.</p><p><pre class="brush: cpp">template&lt;typename Functor&gt;
inline scope_guard&lt;Functor&gt; create_scope_guard(const Functor &amp;f) throw()
{
    return scope_guard&lt;Functor&gt;(const_cast&lt;Functor&amp;&gt;(f));
}</pre></p><p><pre class="brush: text">
C:\test&gt;g&#43;&#43; -Wall -Wextra -std=c&#43;&#43;0x -o main.exe test.cpp
test.cpp: In function 'scope_guard&lt;Functor&gt; create_scope_guard(const Functor&amp;) [with Functor = void()]':
test.cpp:100:45:   instantiated from here
test.cpp:85:53: error: invalid use of const_cast with type 'void (&amp;)()', which is a pointer or reference to a function type
test.cpp:86:1: warning: control reaches end of non-void function
</pre></p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/7f4f60a5fe6f4c5c864c9f390111d4d5#7f4f60a5fe6f4c5c864c9f390111d4d5</link>
		<pubDate>Mon, 08 Aug 2011 16:36:59 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/7f4f60a5fe6f4c5c864c9f390111d4d5#7f4f60a5fe6f4c5c864c9f390111d4d5</guid>
		<dc:creator>Jonas_No</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Jonas_No/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>Okay, I've been doing some reading of N3242, and what VC&#43;&#43; is doing is wrong. The version of the class I presented should not work.</p><p>Both&nbsp;my version and STL's version are initialized using a non-const reference, which should not be able to bind to an rvalue. According to N3242, the result of a lambda expression is a pure rvalue, so you should not be able to call that constructor (or the create_scope_guard) using a lambda. STL's&nbsp;macro gets around that by storing the lambda in a local variable first, so he's not passing an rvalue to the function.</p><p>And come to think of it, using &quot;create_scope_guard(noarg_functor());&quot; shouldn't work either, because a temporary shouldn't bind to an non-const reference either. What's worse, I'm storing the address of that temporary, which is destructed as soon as create_scope_guard returns, so when the scope_guard goes out of scope it will try to use an already destructed object! Again, this doesn't apply to STL's version because if you use the macro, you're never passing a functor to it.</p><p>I find it very peculiar that VC&#43;&#43; lets you bind rvalue temporaries to non-const references, even though the standard explicitly forbids that, even in C&#43;&#43;03.</p><p>Despite circumventing this issue (as long as you use the macro), STL's ScopeWarden class still won't work in g&#43;&#43; of course, due to the use of std::addressof (not yet supported) and __declspec(nothrow) (not standard).</p><p>Therefore, I believe the only way to make this safe to use on function pointers, functors and lambdas is to copy the function object by value. Yes, this may be problematic if the functor is large or has a non-trivial copy ctor, but I don't think there's another safe, standards-compliant way to do this.</p><p>The following uses this approach, and works in VC2010 and g&#43;&#43; 4.5:</p><p><pre class="brush: cpp">template&lt;typename Functor&gt;
class scope_guard
{
public:
    scope_guard(Functor function)
        : _function(function), _valid(true)
    {
    }

    ~scope_guard() throw()
    {
        if( _valid )
        {
            try
            {
                _function();
            }
            catch(...)
            {
                std::terminate();
            }
        }
    }

    scope_guard(scope_guard&lt;Functor&gt; &amp;&amp;other)
        : _function(std::move(other._function)), _valid(other._valid)
    {
        other._valid = false;
    }

    scope_guard&lt;Functor&gt;&amp; operator=(scope_guard&lt;Functor&gt; &amp;&amp;other)
    {
        if( &amp;other != this )
        {
            _function = std::move(other._function);
            _valid = other._valid;
            other._valid = false;
        }
        return *this;
    }
private:
    scope_guard(const scope_guard&lt;Functor&gt;&amp;);
    scope_guard&lt;Functor&gt;&amp; operator=(const scope_guard&lt;Functor&gt;&amp;);

    Functor _function;
    bool _valid;
};

template&lt;typename Functor&gt;
inline scope_guard&lt;Functor&gt; create_scope_guard(Functor function)
{
    return scope_guard&lt;Functor&gt;(function);
}</pre></p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/542ed78d4de6416783599f3a004f0e3c#542ed78d4de6416783599f3a004f0e3c</link>
		<pubDate>Tue, 09 Aug 2011 04:47:49 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/542ed78d4de6416783599f3a004f0e3c#542ed78d4de6416783599f3a004f0e3c</guid>
		<dc:creator>Sven Groot</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Sven Groot/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>&gt; STL's macro gets around that by storing the lambda in a local variable first, so he's not passing an rvalue to the function.</p><p>Yep, ScopeWarden is designed to invoke named functors (including named lambdas). The macro is just for convenience.</p><p>Because ScopeWarden is templated on functor type, it's physically impossible to give it an unnamed lambda.</p><p>Finally, observe that &quot;explicit ScopeWarden(F&amp;&amp;);&quot; is private and unimplemented. That prevents you, at compile-time, from constructing ScopeWarden&lt;F&gt; from an F rvalue.</p><p>(I am *very* careful.)</p><p>&gt; I find it very peculiar that VC&#43;&#43; lets you bind rvalue temporaries to non-const references, even though the standard explicitly forbids that, even in C&#43;&#43;03.</p><p>This is what I refer to as the Evil Extension. Always compile with /W4, which usually warns about any attempt to invoke the Evil Extension.</p><p>&gt; Despite circumventing this issue (as long as you use the macro), STL's ScopeWarden<br>&gt; class still won't work in g&#43;&#43; of course, due to the use of<br>&gt; std::addressof (not yet supported)</p><p>Just say &quot;&amp;f&quot;. std::addressof() was for pedantic correctness, if someone has a functor that also overloads the address-of operator.</p><p>&gt; and __declspec(nothrow) (not standard).</p><p>Just eliminate it - it's an optimization hint - or replace it with noexcept (GCC 4.6&#43;). With noexcept, you don't need the try-catch-terminate in the dtor.</p><p>ScopeWarden is perfectly portable, I just didn't bother to write the minor ifdefs to make it so.</p><p>By the way - the problem with copying by value is that copy ctors can throw exceptions (what if the functor contains a std::string?).&nbsp;Consider what happens if you write:</p><p>do_X();</p><p>[... scope guard g to undo X, whose construction can throw ...]</p><p>do_Y();</p><p>g.dismiss();</p><p>If g's construction (or anything involved in it, like a helper function) throws, then X will remain done without Y being done.&nbsp; That violates transaction semantics, and defeats the whole purpose of a scope guard.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/ea6f4af7f1d54461b70a9f3b001932a9#ea6f4af7f1d54461b70a9f3b001932a9</link>
		<pubDate>Wed, 10 Aug 2011 01:31:44 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/ea6f4af7f1d54461b70a9f3b001932a9#ea6f4af7f1d54461b70a9f3b001932a9</guid>
		<dc:creator>STL</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/STL/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>The strange thing is, the existence of the private &quot;explicit ScopeWarden(F&amp;&amp;);&quot; prevents my template function creation approach from working with regular function pointers. If that declaration isn't there, it just uses the regular constructor, but if it is there, I get an error about not being able to access the private member.</p><p></p><blockquote><div class="quoteText">By the way - the problem with copying by value is that copy ctors can throw exceptions</div></blockquote><p></p><p>I realize that, but so can constructors. If I use your ScopeWarden like this:</p><p></p><pre class="brush: cpp">SomeFunctor f;ScopeWarden&lt;SomeFunctor&gt; warden(f);</pre><p></p><p>The constructor for f can still throw an exception, even if the ScopeWarden cannot. Is there any strong guarantee in the standard that construction of a lambda won't throw an exception?</p><p>Personally, if I were to use a class like this for transaction semantics, I'd probably use it like this:</p><p></p><pre class="brush: cpp">SCOPE_WARDEN(if( isXDone ) UndoX(););DoX();</pre><p></p><p>This circumvents the whole issue of throwing exceptions in the ctor.</p><p>This is similar to how C#'s using puts the variable outside the generated try/finally block, but its instantiation inside, and then in the finally block it checks for null before calling Dispose.</p><p>Ultimately the question here is: how would you&nbsp;define your ScopeWarden to be usable<em>without</em> a macro, but still require only a single statement to use and not require explicitly stating the template parameter? That's what I've been trying to do (more as a thought exercise rather than for anything practical), and what I think Jonas_No's goal was too.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/9fb126dace5647e98bd39f3b00362043#9fb126dace5647e98bd39f3b00362043</link>
		<pubDate>Wed, 10 Aug 2011 03:17:04 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/9fb126dace5647e98bd39f3b00362043#9fb126dace5647e98bd39f3b00362043</guid>
		<dc:creator>Sven Groot</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Sven Groot/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>&gt; The strange thing is, the existence of the private &quot;explicit ScopeWarden(F&amp;&amp;);&quot;<br>&gt; prevents my template function creation approach from working with regular function pointers.</p><p>Are you saying &amp;func? That's an rvalue (observe that &amp;(&amp;func) would be bogus).</p><p>ScopeWarden could be partially specialized for function pointers (it's just obnoxious if you want to handle arbitrary calling conventions).</p><p>&gt; The constructor for f can still throw an exception, even if the ScopeWarden cannot.</p><p>Yeah, ScopeWarden can't defend against that. &quot;Don't do that then.&quot;</p><p>&gt; Is there any strong guarantee in the standard that construction of a lambda won't throw an exception?</p><p>Lambdas with value captures of throwing types obviously can. The Standard appears to be silent about stateless lambdas, and lambdas with only value captures of non-throwing types (e.g. int) and reference captures, but no sane implementation will ever emit exceptions for those.</p><p>&gt; Personally, if I were to use a class like this for transaction semantics, I'd probably use it like this:<br>&gt; SCOPE_WARDEN(if( isXDone ) UndoX()<img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-5.gif?v=c9' alt='Wink' />;DoX();</p><p>Ah, but then X needs to record that it's done.</p><p>&gt; This circumvents the whole issue of throwing exceptions in the ctor.</p><p>The SCOPE_WARDEN macro already avoids that.</p><p>&gt; Ultimately the question here is: how would you define your ScopeWarden to be usablewithout<br>&gt; a macro, but still require only a single statement to use and not require explicitly<br>&gt; stating the template parameter? That's what I've been trying to do (more as a thought<br>&gt; exercise rather than for anything practical), and what I think Jonas_No's goal was too.</p><p>I don't think it can be done, and I thought about this for a long time while writing ScopeWarden.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/a1ffa650794f4087b1219f3c00083180#a1ffa650794f4087b1219f3c00083180</link>
		<pubDate>Thu, 11 Aug 2011 00:29:49 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/a1ffa650794f4087b1219f3c00083180#a1ffa650794f4087b1219f3c00083180</guid>
		<dc:creator>STL</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/STL/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/a1ffa650794f4087b1219f3c00083180">6 hours&nbsp;ago</a>,<a href="/Niners/STL">STL</a> wrote</p><p>&nbsp;Are you saying &amp;func? That's an rvalue (observe that &amp;(&amp;func) would be bogus).</p></div></blockquote><p></p><p>No, I'm doing the following:</p><p></p><pre class="brush: cpp">template&lt;typename Functor&gt;class TestClass{public:    TestClass(Functor &amp;f) : _f(&amp;f) { }private:    TestClass(Functor &amp;&amp;); // (*)    Functor *_f;};template&lt;typename Functor&gt;inline TestClass&lt;Functor&gt; Create(Functor &amp;f){    return TestClass&lt;Functor&gt;(f);}void Foo(){}int main(){    auto test = Create(Foo);    return 0;}</pre><p></p><p>This gives the error message &quot;error C2248: 'TestClass&lt;Functor&gt;::TestClass' : cannot access private member declared in class 'TestClass&lt;Functor&gt;'&quot;.</p><p>But if you remove the line marked with (*), it works fine.</p><p></p><blockquote><div class="quoteText">I don't think it can be done, and I thought about this for a long time while writing ScopeWarden.</div></blockquote><p></p><p>I expect you're right, at least without accepting the use of the copy ctor. Of course, you could simply mandate &quot;don't use types that can throw in the copy ctor&quot; and be done with it.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/80bdb529e2714033bc019f3c007d01f9#80bdb529e2714033bc019f3c007d01f9</link>
		<pubDate>Thu, 11 Aug 2011 07:35:08 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/80bdb529e2714033bc019f3c007d01f9#80bdb529e2714033bc019f3c007d01f9</guid>
		<dc:creator>Sven Groot</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Sven Groot/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>In other words: c&#43;&#43; is limited in this area.<br>Like i said a few posts ago.</p><p>That is still sad to hear. <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-6.gif?v=c9' alt='Sad' /></p><p>&nbsp;</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/8a5228c4c1664c0ca5c39f45008e5eb9#8a5228c4c1664c0ca5c39f45008e5eb9</link>
		<pubDate>Sat, 20 Aug 2011 08:38:21 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/8a5228c4c1664c0ca5c39f45008e5eb9#8a5228c4c1664c0ca5c39f45008e5eb9</guid>
		<dc:creator>Mr Crash</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Mr Crash/Discussions/RSS</wfw:commentRss>
	</item>
</channel>
</rss>