Hansi | 22 May 15:24
Picon

design question with using call_traits and functors

Hello,

I want to make a functor which uses automatically an reference type if 
the provided value isn't a pointer.

Now I have thougth to use the call_traits library.

template<typename Value>
struct Setter
{
	typedef void result_type;

	void operator()(const std::wstring& newVal, Value* pVal)
	{
		*pVal = boost::lexical_cast<typename Value>(newVal);
	}

	void operator()(const std::wstring& newVal, typename 
boost::call_traits<Value>::reference refVal)
	{
		refVal = boost::lexical_cast<typename Value>(newVal);
	}
};

To call the function I have the following statement:

SetValue setter = boost::bind(Setter<short>(),_1,x);

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 
(Continue reading)

Nat Goodspeed | 22 May 22:31
Favicon

Re: design question with using call_traits and functors

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[])
(Continue reading)

Hansi | 23 May 07:57
Picon

Re: design question with using call_traits and functors


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);
> }
(Continue reading)

Nat Goodspeed | 23 May 23:07
Favicon

Re: design question with using call_traits and functors

Hansi wrote:

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

Um - I don't understand the bad case you're imagining. I added this:

void func(int& param)
{
     Set("51", param);
}

and these two lines:

     func(target);
     std::cout << "Set(ref) produced " << target << '\n';

and got what I expected. So I'm not yet following your train of thought.

Gmane