
On Fri, May 7, 2010 at 11:10 AM, Eric Niebler
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.
Thanks for the quick response and the nice explanation.
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:
That would work, except that I would need 2^n versions for operator() taking 'n' arguments. (Yay! I discovered the perfect forwarding problem all by myself :-/). -- Manjunath http://nonchalantlytyped.net/blog/musings/