# Why \caption conflicts with beamer?

I am attempting to use package caption with beamer. The MWE below runs fine. If I uncomment captionof, I am presented with message you cannot use '\unskip' in vertical mode. To be even more enigmatic, if I change to document class memoir, even with captionof compiles fine. What is going on here?

\documentclass{beamer}
%\documentclass{memoir}

\usepackage{listingsutf8}   % Use program listings
\usepackage{caption}

\makeatletter
\lstinputlisting{#2}
%   \captionof{lstlisting}{#3}%
}
\makeatother

\begin{document}
\end{document}

% % % % % % % % % % % % % % % % % The sumvec file, for completeness
int sumvec(int v[N])
{
int i, sum = 0;
for (i = 0; i < N; i++) longer line to test
sum += v[i];
return sum;
}
• Beamer is is very own thing so you really should not expect it to work with packages that change core stuff. Why do you want to use the caption package with beamer in the first place? – daleif May 12 '17 at 13:11
• Btw listing already have a caption option build in. – daleif May 12 '17 at 13:12
• Is the built-in option compatible with babel? – katang May 12 '17 at 20:29

I don't see any conflict, you just have to use a frame environment:

\begin{frame}
\end{frame}

Full MWE:

\begin{filecontents}{sumvec.c}
int sumvec(int v[N])
{
int i, sum = 0;
for (i = 0; i < N; i++) longer line to test
sum += v[i];
return sum;
}
\end{filecontents}

\documentclass{beamer}

\usepackage{listingsutf8}
\usepackage{caption}