
On 5/7/2010 10:55 AM, Manjunath Kudlur wrote:
Now, given an expression type E, I can say "typename boost::result_of
(const E &)>::type" and it will be either "T0 &" or "const T0 &" depending on how _1 appears in the expression E. Using this, I tried to write an expression wrapper with operator() overloaded as follows : template<typename T0> void operator()(typename boost::result_of
(const Expr &)>::type arg0) const { cout << "arg0 " << arg0 << "\n"; } But the compiler fails to find a match with this overloaded version when I write, say, (_1)(10). Is it some fundamental C++ restriction that causes this, or am I missing something?
There's a fundamental restriction. T0 is in a nondeduced context here.
Imagine a template Int like:
template<typename T> struct Int { typedef int type; };
and used like
template<typename T>
void foo(typename Int<T>::type) ...
foo(1);
What can the compiler say about T? Any choice will work, so the compiler
has no way to pick one.
What you need to do instead is define both const and non-const overloads
and either use SFINAE to get one to drop out, or else (better, IMO)
assert within the function that the correct one has been chosen:
template<typename T0>
void operator()(T0 & arg0) const
{
BOOST_MPL_ASSERT((
is_same<
T0 &,
typename boost::result_of