Tech Off Post

Single Post Permalink

View Thread: C++0x (vs2010) question, please tell me what i'm doing wrong
  • User profile image
    Mr Crash

    > "template argument deduction". This applies only to template functions, not to template classes.

    I need to refresh my C++ knowledge.
    I find it strange and kind of annoying that "template argument deduction" doesn't work for template classes.
    I wonder what the reason was for this rule..

    Yes, i did find that 'std::function' made it work but thought there must be a better way, lack of "template argument deduction" explains it.

    What i'm trying to do is to create a template class that does stuff when it leaves scope:
    Some unoptimized examples:
    scoped<HMODULE> dll(LoadLibrary("kernel32.dll"), [](HMODULE h){FreeLibrary(h);})

    scoped<int*> ptr(new int, [](int *p){delete p;})
    scoped<int[]> ptr(new int[100], [](int *p){delete [] p;})

    scoped<int> ptr(1974, [](int p){ cout << p << endl;})

    stuff like that.
    I know of unique_ptr but i wanted to create a single template class that could do all of the above.

    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 "test_f tests" for example.

    It seems to be very tricky to get it to work.
    One of the things i would like to minimize is the amount of typing required but the "template argument deduction" not for template classes rule, is in the way of that. auto keyword does n't help either.

    Input, ideas, etc. are much appreciated.