public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "zeratul976 at hotmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/54310] New: Order of operations during overload resolution
Date: Sat, 18 Aug 2012 08:04:00 -0000	[thread overview]
Message-ID: <bug-54310-4@http.gcc.gnu.org/bugzilla/> (raw)

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54310

             Bug #: 54310
           Summary: Order of operations during overload resolution
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: zeratul976@hotmail.com


GCC accepts the following code:


template <typename T>
struct meta
{
    typedef typename T::type type;
};

struct S{};

template <typename T>
typename meta<T>::type foo(T, S);

int foo(int, int);      

int main()
{
    foo(0, 0);
}


Clang rejects this code with the following error:

test.cpp:4:22: error: type 'int' cannot be used prior to '::' because it has no
members
    typedef typename T::type type;
                     ^
test.cpp:10:10: note: in instantiation of template class 'meta<int>' requested
here
typename meta<T>::type foo(T, S);
         ^
test.cpp:10:24: note: while substituting deduced template arguments into
function template 'foo' [with T = int]
typename meta<T>::type foo(T, S);
                       ^

I believe the code is invalid (and clang's error is correct), for the following
reasons:

1. Template argument deduction should be performed on the template candidate
*before* to discarding it due to a type mismatch for the second parameter
(expected S, got int). Section 13.3.1/7 of the standard says (emphasis mine):
"In each case where a candidate is a function template, candidate function
template specializations are generated using template argument deduction. Those
candidates are *then* handled as candidate functions in the usual way. A given
name can refer to one or more function templates and also to a set of
overloaded non-template functions. In such a case, the candidate functions
generated from each function template are combined with the set of non-template
candidate functions."

2. Template argument deduction on the template candidate should fail with a
hard error (not SFINAE), because the error that occurs (T::type not being valid
for T = int) is not in the immediate context of the function type. (Section
14.8.2/8, emphasis mine: "If a substitution results in an invalid type or
expression, type deduction fails. [...] Only invalid types and expressions *in
the immediate context* of the function type and its template parameter types
can result in a deduction failure. [...]").


GCC does reject the following example:


template <typename T>
struct meta
{
    typedef typename T::type type;
};

template <typename T>
typename meta<T>::type foo(T);

int foo(int);      

int main()
{
    foo(0);
}


With the following error:

test.cpp: In instantiation of 'struct meta<int>':
test.cpp:8:24:   required by substitution of 'template<class T> typename
meta::type foo(T) [with T = int]'
test.cpp:14:10:   required from here
test.cpp:4:30: error: 'int' is not a class, struct, or union type
     typedef typename T::type type;
                              ^

suggesting that GCC already obeys point (2) above, and therefore the problem is
likely to be with point (1).


             reply	other threads:[~2012-08-18  8:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-18  8:04 zeratul976 at hotmail dot com [this message]
2013-07-07 17:35 ` [Bug c++/54310] " zeratul976 at hotmail dot com
2013-08-21 10:25 ` paolo.carlini at oracle dot com
2014-05-13 15:44 ` paolo.carlini at oracle dot com
2014-05-13 16:14 ` paolo at gcc dot gnu.org
2014-05-13 16:15 ` paolo.carlini at oracle dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-54310-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).