dimanche 8 mars 2020

Sfinae and template specialization

I want to provide a function in my library:

namespace Foo {
template<typename G>
typename std::enable_if<std::is_fundamental<G>::value, std::vector<uint8_t>>::type toBytes(const G &g) {
//something here
}
}

However I want to provide basic implementation only for basic types. If an user wants to add a specialization I thought it was enought to write:

namespace Foo {
template<>
std::vector<uint8_t> toBytes<struct A>(const struct A &g) {
//something here
}
}

However it doesn't compile, Gcc provides this error:

std::vector Foo::toBytes(const A&)’ does not match any template declaration

Am I missing anything?

Aucun commentaire:

Enregistrer un commentaire