c++ - let function return template type -
i had following function
slabwallconnectionevent* createobservedevent(slabwallconnectionevent::data* peventdata) { assert(peventdata != nullptr); return slabwallconnectionevent::create(peventdata->m_wall, peventdata->m_slab); }
then able make more general template template specification.
.h file
template <class observedeventtype, class observedeventdatatype> void createobservedevent(observedeventtype*& pevent,observedeventdatatype* peventdata);
.cpp file
template<> void createobservedevent<slabwallconnectionevent, slabwallconnectionevent::data> (slabwallconnectionevent*& pevent, slabwallconnectionevent::data* peventdata) { assert(peventdata != nullptr); pevent = slabwallconnectionevent::create(peventdata->m_wall, peventdata->m_slab); }
this works me not pleased reference raw pointer. possible change this:
.h file
template <class observedeventtype, class observedeventdatatype> observedeventtype* createobservedevent(observedeventdatatype* peventdata);
.cpp file
template<> slabwallconnectionevent* createobservedevent<slabwallconnectionevent, slabwallconnectionevent::data> (bim_ui::slabwallconnectionevent::data* peventdata) { assert(peventdata != nullptr); return bim_ui::slabwallconnectionevent::create(peventdata->m_wall, peventdata->m_slab); }
when try use following error:
error 1 error c2783: 'observedeventtype *createobservedevent(observedeventdatatype *)' : not deduce template argument 'observedeventtype'
edit
use function in template function
template <class observedeventtype> void cmdbimdrag::observeconnectionevent(observedeventdata* pobservedevent){ ... if (pcurrentevent == nullptr) createobservedevent(pcurrentevent, peventdata); ... }
and function gets called this
observeconnectionevent<slabwallconnectionevent>((*pobservedeventdata).get());
edit2
jonas' answers first part works specifying template type enough:
createobservedevent<slabwallconnectionevent>(peventdata);
however using auto not seem warrant removal of return type spefication:
.h file
template <class observedeventtype, class observedeventdatatype> auto createobservedevent(observedeventdatatype* peventdata) -> observedeventtype*;
.cpp file
template<> auto createobservedevent<slabwallconnectionevent, slabwallconnectionevent::data> (slabwallconnectionevent::data* peventdata) -> slabwallconnectionevent* { assert(peventdata != nullptr); return slabwallconnectionevent::create(peventdata->m_wall, peventdata->m_slab); }
this code compiles template specification calling:
createobservedevent(peventdata);
still gives first error not being able deduce type.
you have specific return type:
createobservedevent<slabwallconnectionevent>(peventdata);
here example, using simpler types.
alternatively, use auto
return type , not include template parameter return type. online example here.
Comments
Post a Comment