\footnotes in environment with \pushftn \popftn

Code proposed by David Carlisle works out-of-the box, cause has no package dependencies. It's comfortable solution of dealing with footnotes in environments needing \footnotemark+footnotetext.

I have no problems in using it with \vbox and itemize.

However when I try to apply it to centred tabular I encounter problem - footnotes are not rendered, only footmarks:

\documentclass{report}

%Thanks to David Carlisle for pushftn and popftn : http://tex.stackexchange.com/a/43695/7128
\makeatletter
\newtoks\FTN@ftn
\def\pushftn{%
\let\@footnotetext\FTN@ftntext\let\@xfootnotenext\FTN@xftntext
\let\@xfootnote\FTN@xfootnote}
\def\popftn{%
\global\FTN@ftn\expandafter{\expandafter}\the\FTN@ftn}
\long\def\FTN@ftntext#1{%
\edef\@tempa{\the\FTN@ftn\noexpand\footnotetext
[\the\csname c@\@mpfn\endcsname]}%
\global\FTN@ftn\expandafter{\@tempa{#1}}}%
\long\def\FTN@xftntext[#1]#2{%
\global\FTN@ftn\expandafter{\the\FTN@ftn\footnotetext[#1]{#2}}}
\def\FTN@xfootnote[#1]{%
\begingroup
\csname c@\@mpfn\endcsname #1\relax
\unrestored@protected@xdef\@thefnmark{\thempfn}%
\endgroup
\@footnotemark\FTN@xftntext[#1]}

\makeatother

\begin{document}

\pushftn
\begin{table}[htbp]
\begin{center}
\begin{tabular}{ll}
\hline
X & Y \\
\hline
\hline
a & b \footnote{AB} \\
\hline
c & d \footnote{CD} \\
\hline
\end{tabular}
\end{center}
\end{table}
\popftn

\end{document}


What am I doing wrong?

-
The table environment builds a box; so \pushftn must go inside it. Of course this will fail if the table is typeset as a page float. –  egreg Feb 8 '12 at 15:39
Also you might want to replace \begin{center} by \centering and delete \end{center}. –  Stephen Feb 8 '12 at 18:22

Ah, you had me confused there for a while. Stick \begingroup before the push and \endgroup before the pop.
Note though that lifting the footnotes out of the floating table is inherently fragile, if the table floats off to another page, the footnotes will not float with it, and if it ends on a p float page, LaTeX does not support footnotes on such a page.
Thank you. Now it works :) \begingroup and \endgroup solves problem. Your description clarifies me how all this works. Good point of not using footnotes inside floats, I've forgot about this. –  Grzegorz Wierzowiecki Feb 8 '12 at 16:41