|
Boost Users : |
From: David Walthall (walthall_at_[hidden])
Date: 2008-03-07 13:10:04
Derrick Hathaway wrote:
> I apologize if this isn't the sort of question this list is intended
> for, but if anyone could help me with my lack of sufficient template
> howto skill I would appreciate it.
>
<snip>
> The problem is when I compile I get the error message "could not
> deduce template argument for 'ValueType'" when I compile. Any idea
> what I'm doing wrong? I have attached example code. I am using MS
> Visual Studio 8, and boost 1.34.1.
>
> Thanks in advance.
>
> -Derrick
Hi Derrick,
The problem is that you are trying to get the compiler to deduce what
instantiation of predicate is in use based on the nested type. That is,
your code is requesting that the compiler figure out ValueType based on
the boost::function1 type. From what I can understand, the standard
forbids this since in general this is not possible. There is a post
here that goes into more detail:
http://www.thescripts.com/forum/thread62685.html
I can come up with only a few options:
(1) Tell the compiler which version to use. It works but is very verbose:
predicate<foo>::type lt_and_gt(operator &<foo>(lt,gt));
However, if you are going to do that, it is easier to rename operator &
to And() so that you can write:
predicate<foo>::type lt_and_gt(And<foo>(lt,gt));
(2) Rewrite operator & to be deducible:
#include <boost/type_traits.hpp>
template <typename pred_type>
pred_type operator &(const pred_type &left, const pred_type &right)
{
typedef typename boost::remove_const<typename
pred_type::argument_type>::type constless_type;
typedef typename boost::remove_reference<constless_type>::type
ValueType;
return typename predicate<ValueType>::and_(left, right);
}
While this works and allows for your original syntax, the extraction of
ValueType from the boost::function seems excessively brittle to me.
By the way, I think that 'and' and 'or' are C++ keywords. See:
http://www.gotw.ca/gotw/086.htm
In addition, you'll need to add const to greater_than and less_than
functions to get this to compile.
David
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