Hansi wrote:
I want to make a functor which uses automatically an reference type if
the provided value isn't a pointer.
Now the only thing that I don't like very much is that the user has to
provide the template argument for setter. Is there some possibility to
solve it that the template argument is automatically deduced?
Important is that I have to possible calls one when the value is
provided as value -> I need to have a reference parameter and the other
is when a pointer is provided.
The following works for me:
#include <iostream>
#include
template<typename Value>
void Set(const std::string& newVal, Value* pVal)
{
*pVal = boost::lexical_cast<Value>(newVal);
}
template<typename Value>
void Set(const std::string& newVal, Value& refVal)
{
refVal = boost::lexical_cast<Value>(newVal);
}
int main(int argc, char *argv[])
{
int target;
Set("17", &target);
std::cout << "Set(Value*) produced " << target << '\n';
Set("34", target);
std::cout << "Set(Value&) produced " << target << '\n';
return 0;
}
If you want those overloaded template free functions to forward to a
functor, that should be fine too: as you see, inside them you can
explicitly name the inferred type.