boost::function "could not deduce template argument ..."

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. Essentially what I have is class template for boost::function1<bool, const ValueType &> like so: template <typename ValueType> class predicate { public: typedef boost::function1<bool, const ValueType&> type; struct and { bool operator()(const ValueType &)() { return left_() && right(); } type left_; type right_; }; struct or { ... }; }; And what I want is to overload the & and | operators for predicate<ValueType>::type like so: template <typename ValueType> typename predicate<ValueType>::type operator &( const typename predicate<ValueType>::type &left, const typename predicate<ValueType>::type &right) { return typename predicate<ValueType>::and(left, right); } 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

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
participants (2)
-
David Walthall
-
Derrick Hathaway