On reading code online from production libraries I found something like this
Traits.hpp
template <typename Type>
class Traits {
template <typename T,
detail::EnableIfIsInstantiation<T, Type>* = nullptr>
static void foo(T& object) {
object.foo();
}
};
SpecialTraits.hpp
template <>
class Traits<Special> {
static void foo(Special& object) {
object.foo();
}
static void foo(Special&& object) {
object.special_foo();
}
};
This will cause an ODR violation if a library instantiates a type that uses Traits
for Something
in one translation unit without including SpecialTraits.hpp
and then instantiates a type that uses the specialized traits in another translation unit. This would cause an ODR violation when those two translation units are linked together.
What is the suggested way to avoid this problem? Do I have to resort to including all the specializations in the original Traits.hpp
file? And what if I am not allowed to edit the file with the definition for Special
?
Note Please ignore the fact that foo()
could have been specialized by Special
itself in the &&
case. I could not think of a better example..
Special
. – Mediationboost
module – Carraratemplate<> class Traits<Special>;
to let the compiler know the specialization exists and not to instantiate it from the primary template. Not that this helps that much if you can't put it in the header Traits.hpp. – Crappie