c++ - Partial specialization for pointer as function return type -


i have template wrapper function returns value this:

template<class t> t foo(bar& bar, const char* key) {     return bar.value<t>(key); } 

but want handle pointer types bit different, this:

template<class t> t foo(bar& bar, const char* key) {     return (t)bar.value<void*>(key); } 

so can do:

int x = foo<int>(bar, "x"); baz* b = foo<baz*>(bar, "b"); 

writing above gives me error because of multiple definitions. there other way this? prefer not add cast each function uses pointer.

i've tried following:

template<class t> t foo(bar& bar, const char* key) {     if(std::is_pointer<t>::value)         return (t)bar.value<void*>(key);     else         return bar.value<t>(key); } 

but doesn't work either because there qvariants involved , produce error instantiating 1 of template functions unknown pointer type (the bar.value<t>).

use tag dispatching delegate calls helper functions different things based on whether t pointer type or not.

namespace detail { template<class t> t foo(bar& bar, const char* key, std::false_type /*not is_pointer*/) {     return bar.value<t>(key); }  template<class t> t foo(bar& bar, const char* key, std::true_type /*is_pointer*/) {     return (t)bar.value<void*>(key); } }  template<class t> t foo(bar& bar, const char* key) {     return detail::foo<t>(bar, key, std::is_pointer<t>{}); } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -