Call template function for the value of a pointer out of a template function, in C++ -
i trying call template function pointer given template parameter in calling function. code is:
template <> struct serialize_helper<std::string> { // not important code... } }; template <class t> inline void serializer(const t& obj, streamtype::iterator& res) { if(std::is_pointer<t>::value) { //this doesn' work serialize_helper<*t>::apply(*obj,res); } else serialize_helper<t>::apply(obj,res); } if call:
std::string test("test"); serializer(test, res); everything works fine. want able call serializer pointer obj:
std::string test* = new std::string("test"); serializer(test, res); dereferencing pointer before calling serializer function not possible option, please not propose that. inside serializer function possible.
shorter description: want call serializer std::string* , have same thing has if i'd called std::string points to.
the whole body of template function needs compile types it's instantiated with, regardless of whether or not branch ever taken. around issue, can define separate functions when t pointer , when it's not.
using sfinae:
template <class t, std::enable_if_t<std::is_pointer<t>::value>* = nullptr> inline void serializer(const t& obj, streamtype::iterator& res) { serialize_helper<std::remove_pointer_t<t>>::apply(*obj,res); } template <class t, std::enable_if_t<!std::is_pointer<t>::value>* = nullptr> inline void serializer(const t& obj, streamtype::iterator& res) { serialize_helper<t>::apply(obj,res); } using tagged-dispatch:
template <class t> inline void serializer(const t& obj, streamtype::iterator& res, std::true_type) { serialize_helper<std::remove_pointer_t<t>>::apply(*obj,res); } template <class t> inline void serializer(const t& obj, streamtype::iterator& res, std::false_type) { serialize_helper<t>::apply(obj,res); } template <class t> inline void serializer(const t& obj, streamtype::iterator& res) { serializer(obj, res, std::is_pointer<t>()); }
Comments
Post a Comment