Home > Mobile >  Standard hash with variadic template
Standard hash with variadic template

Time:03-10

I have the following code:

namespace foo {
template<typename ...Types>
class Pi {
};
}

namespace std {
        template<> //line offending gcc 8.3.1
        template<typename ...Types>
        struct hash<foo::Pi<Types...>> {
            std::size_t operator()( const foo::Pi<Types...>& s ) const noexcept {
              return 0;
            }
        };
}


int main() {
   return 0;
}

Using gcc 8.3.1 I receive the error too much parameters template-parametr-list, while using gcc 4.8.3 it works. If I remove the commented line above it works, is it correct however?

CodePudding user response:

The newer GCC version is correct.

template<> is not valid syntax here. It is only used for explicit specialization of a template.

What you are doing here is partial specialization of std::hash, since you are not specializing for just one specific type, but all types that could be instantiated from foo::Pi. Without template<> you have the correct syntax for partial specialization.

  • Related