Member Function Specialization in Class Templates
Partial specialization, allowing for the modification of specific members in a template class, is not supported for member functions. This means that code like the following will not compile:
<code class="cpp">template <typename T, bool B> struct X { void Specialized(); }; template <typename T> void X<T, true>::Specialized() { ... } template <typename T> void X<T, false>::Specialized() { ... }</code>
Alternative Approaches
<code class="cpp">template <> void X<int, true>::Specialized() { ... }</code>
<code class="cpp">template <typename T, bool B> struct X { template <bool B> static void Specialized(int); }; template <typename T> inline void X<T, true>::Specialized(int) { ... } template <typename T> inline void X<T, false>::Specialized(int) { ... }</code>
<code class="cpp">template <typename T, bool B> struct SpecializedImpl { static void call() { ... } }; template <typename T, bool B> struct X { void Specialized() { SpecializedImpl<T, B>::call(); } };</code>
The above is the detailed content of Can Member Functions Be Partially Specialized in Class Templates?. For more information, please follow other related articles on the PHP Chinese website!