lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] Difficulty writing a generic contains() function template


From: Greg Chicares
Subject: Re: [lmi] Difficulty writing a generic contains() function template
Date: Tue, 11 May 2010 19:09:19 +0000
User-agent: Thunderbird 2.0.0.24 (Windows/20100228)

On 2010-05-05 13:37Z, Vadim Zeitlin wrote:
[...]
>  Whether it's really better than just defining a traits type and
> specializing it for all the container types is open to discussion. Notice
> that it took me more than half an hour to get this right...

That's much less time than it would have taken me, so let me ask a question.

> template <class T>
> struct has_find_method {
>     struct BaseFind { void find(); };
>     struct Base : T, BaseFind {};
> 
>     template <typename FP, FP fp> struct Tester;
> 
>     template <class U>
>     static char test(U *, Tester<void (BaseFind::*)(), &U::find>* = 0);
>     static long test(...);
> 
>     static const bool value = sizeof(test(static_cast<Base *>(0))) != 1;
> };

As far as I understand it, SFINAE comes into play here because the
template form of test() is ambiguous when T has a find() member.
I.e., this simple self-contained testcase:

  struct With {void find(int) const{}};

  struct Mixin {void find();};
  struct Derived : With, Mixin {};

  int test_main(int, char*[])
  {
      Derived d;
      d.find();

      return 0;
  }

fails to compile because Derived has two distinct find() members:

  request for member `find' is ambiguous
  candidates are: void Mixin::find()
                  void With::find(int) const

What I don't understand is why ambiguity causes substitution failure,
at least under ISO/IEC 14882 2003-10-15 [14.8.2/2]. I don't see which
of the "--Attempting to..." subparagraphs would address ambiguity.
Am I missing something?

OTOH, N3092 of 2010-03-26 [14.8.2.8] says deduction fails
  "if a substitution results in an invalid type or expression"
and the small testcase above certainly does seem to involve an invalid
expression. So does this technique actually depend on changes to the
language standard that have not yet formally been adopted? I'm trying
to understand how "exotic" this is.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]