This should work with all classes. The idea is to call
\advanced\section
for the special sectional units (any level, so long as it's numbered).
How is it done? When \advanced\section is followed, the meaning of \thesection is saved and modified by adding \advancedmarker at the end. After the title is typeset, the meaning of \thesection is restored.
THe \advancedmarker command prints a zero width asterisk when printing the sectional title and in the table of contents, but a normal asterisk in all other situations.
Bonus feature: if one doesn't want the asterisk when \ref is used, it's sufficient to change the definition of \advancedmarker to
\NewDocumentCommand{\advancedmarker}{}
{
\bool_if:NT \__advanced_killwidth_bool { \makebox[0pt][l]{*} }
}
and \ref{test} will not print any asterisk.
The complete code.
\documentclass{report}
\ExplSyntaxOn
\NewDocumentCommand{\advanced}{mO{#3}m}
{% #1 is a sectioning command
% save the current meaning of \the<level>
\cs_set_eq:Nc __advanced_save: { the \cs_to_str:N #1 }
% add \advancemarker
\cs_set:cpn { the \cs_to_str:N #1 } { __advanced_save: \advancedmarker }
\bool_set_true:N __advanced_killwidth_bool
#1[#2]{#3}
\bool_set_false:N __advanced_killwidth_bool
% reset \the<level> to the previous meaning
\cs_set_eq:cN { the \cs_to_str:N #1 } __advanced_save:
}
\NewDocumentCommand{\advancedmarker}{}
{
\bool_if:NTF __advanced_killwidth_bool { \makebox[0pt][l]{} } { }
}
\bool_new:N __advanced_killwidth_bool
\AddToHook{cmd/@starttoc/begin}{\bool_set_true:N __advanced_killwidth_bool}
\ExplSyntaxOff
\begin{document}
\tableofcontents
\chapter{Test}
Beware that \ref{test} is a difficult section.
\section{This is a normal section}
\advanced\section{This is an optional advanced section}\label{test}
\section{This is a normal section}
\subsection{With a normal subsection}
\advanced\subsection{And an advanced subsection}
\subsection{And a normal subsection}
\advanced\chapter{A difficult chapter}
\end{document}
The TOC

The first chapter (standard)

The second chapter (advanced)

titlesecsolution for ‘variants’: see§4.3 Variants, pp.12-13 of the documentation. – Bernard Oct 18 '19 at 09:33