What’s 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() can’t be called before variable g expired. Compiled at VC++ 2012 and got the same problem both in debug and release mode.
Your
guardkeeps a reference, but it takes a value. The reference becomes invalid as soon asguard‘s constructor end, as it refers to thelastparameter taken by the constructor and not to the parameter passed tomake_guard.Once you access an invalid reference you have undefined behavior, and all bets are off.