Boost logo

Boost Users :

From: Hansi (hansipet_at_[hidden])
Date: 2008-05-23 01:57:54


Nat Goodspeed schrieb:
> 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 <boost/lexical_cast.hpp>
>
> 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;
> }

This version I have tested also, the only Problem which could happen I
think is the reference of reference problem. Isn't it?

Best regards
Hansjörg

>
> 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.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net