Hello fellow C++0x programmers.

 

I have this code:

void normal_func(int p)
{
    _tprintf_s(_T("p: %d\n"), p);
}

struct normal_struct_func
{
    inline void operator()(int p) const
    {
        _tprintf_s(_T("p: %d\n"), p);
    }
};

template<typename T, typename Function>
void test_f(T p, Function func)
{
    func(p);
}

template<typename T, typename Function>
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("p: %d\n"), 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<int> tc1(11, [](int p){_tprintf_s(_T("p: %d\n"), p);});
    //test_c<int> tc2(22, normal_func);
    //test_c<int> tc3(33, normal_struct_func());

    // error C2664: 'test_c<T,Function>::test_c(T,Function (__cdecl *))' : cannot convert parameter 2 from '`anonymous-namespace'::<lambda1>' to 'void (__cdecl *)(int)'
    // ...
    test_c<int, void(int)> tc1(11, [](int p){_tprintf_s(_T("p: %d\n"), p);});
    test_c<int, void(int)> tc2(22, normal_func);
    test_c<int, void(int)> tc3(33, normal_struct_func());

    return 0;
}

 

The 'test_f' works as it should but test_c class, which does the same basically, does not.

 

Problem list:

1. How to make this compile. What am i missing / doing wrong.

2. Why do i have to type "test_c<int, void(int)>" instead of just "test_c<int>", the compiler should be able to figure that out, it did that for "test_f"

 

Thanks for your help.