What is wrong with this code below and how to fix it.
#include<iostream> using namespace std; template<typename Func1, typename Func2> class guard{ public: guard(Func1 first, Func2 last) : last(last){ first(); } ~guard(){ last(); } private: Func2& last; }; template<typename Func1, typename Func2> guard<Func1, Func2> make_guard(Func1 first, Func2 last){ return guard<Func1, Func2>(first, last); } void first(){ cout << "first" << endl; } void last(){ cout << "last" << endl; } int main(){ { first(); // ok last(); // ok auto g = make_guard(first, last); first(); //exception: Access violation last(); //exception: Access violation } first(); // ok last(); // ok cin.get(); }
The function first() and last() cannot be called before the expiration of the variable g . Compiled in VC ++ 2012 and got the same problem both in debug mode and in release mode.
user955249
source share