public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/56421] New: Non-matching overload produces template substitution error
@ 2013-02-21 20:02 kristian.spangsege at gmail dot com
  2013-02-22  9:09 ` [Bug c++/56421] " mpolacek at gcc dot gnu.org
                   ` (16 more replies)
  0 siblings, 17 replies; 18+ messages in thread
From: kristian.spangsege at gmail dot com @ 2013-02-21 20:02 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 56421
           Summary: Non-matching overload produces template substitution
                    error
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: kristian.spangsege@gmail.com


template<class S> struct Foo {
  typedef typename S::type type;
};

template<class> void foo();
template<class S> typename Foo<S>::type foo(int);

int main()
{
  foo<int>();
}


Produces the following error in both gcc-4.7.2 and gcc-4.6.3:

$ g++ -c t.cpp
t.cpp: In instantiation of ‘struct Foo<int>’:
t.cpp:6:41:   required by substitution of ‘template<class S> typename Foo::type
foo(int) [with S = int]’
t.cpp:10:12:   required from here
t.cpp:2:28: error: ‘int’ is not a class, struct, or union type


Clang accepts it as valid.


Note: No errors are reported by GCC if we change line 6 from

  template<class S> typename Foo<S>::type foo(int);

to

  template<class S> typename S::type foo(int);


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
@ 2013-02-22  9:09 ` mpolacek at gcc dot gnu.org
  2013-02-22 10:42 ` redi at gcc dot gnu.org
                   ` (15 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2013-02-22  9:09 UTC (permalink / raw)
  To: gcc-bugs


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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> 2013-02-22 09:08:53 UTC ---
Also with trunk:
c.C: In instantiation of ‘struct Foo<int>’:
c.C:6:41:   required by substitution of ‘template<class S> typename
Foo<S>::type foo(int) [with S = <missing>]’
c.C:10:12:   required from here
c.C:2:28: error: ‘int’ is not a class, struct, or union type
   typedef typename S::type type;
                            ^
For that <missing>, see PR56377.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
  2013-02-22  9:09 ` [Bug c++/56421] " mpolacek at gcc dot gnu.org
@ 2013-02-22 10:42 ` redi at gcc dot gnu.org
  2013-02-22 10:59 ` daniel.kruegler at googlemail dot com
                   ` (14 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2013-02-22 10:42 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-02-22 10:42:05 UTC ---
Isn't G++ correct? Foo::type exists unconditionally, so SFINAE doesn't apply.
The invalid type is not in the immediate context of the substitution.

To make this work you would have to only define Foo::type if S::type exists.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
  2013-02-22  9:09 ` [Bug c++/56421] " mpolacek at gcc dot gnu.org
  2013-02-22 10:42 ` redi at gcc dot gnu.org
@ 2013-02-22 10:59 ` daniel.kruegler at googlemail dot com
  2013-02-22 16:25 ` redi at gcc dot gnu.org
                   ` (13 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2013-02-22 10:59 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #3 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2013-02-22 10:59:28 UTC ---
(In reply to comment #2)
> Isn't G++ correct? Foo::type exists unconditionally, so SFINAE doesn't apply.
> The invalid type is not in the immediate context of the substitution.

This was my initial reaction, too, but there is one point in this example where
I need to find clarification: Both foo() templates have a *different* function
parameter lists (one void, one with int). I'm yet unsure (due to lack of time
studying whether 14.8.2 requires to check first parameter compatibility in the
call context or not).


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (2 preceding siblings ...)
  2013-02-22 10:59 ` daniel.kruegler at googlemail dot com
@ 2013-02-22 16:25 ` redi at gcc dot gnu.org
  2013-02-22 18:05 ` kristian.spangsege at gmail dot com
                   ` (12 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2013-02-22 16:25 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-02-22 16:24:42 UTC ---
I think 13.3 [over.match] ¶2 and 13.3.1 [over.match.funcs] ¶7  say the function
template specialization must be generated before the number of arguments is
checked to see if the candidate function is viable.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (3 preceding siblings ...)
  2013-02-22 16:25 ` redi at gcc dot gnu.org
@ 2013-02-22 18:05 ` kristian.spangsege at gmail dot com
  2013-02-22 18:48 ` redi at gcc dot gnu.org
                   ` (11 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: kristian.spangsege at gmail dot com @ 2013-02-22 18:05 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #5 from Kristian Spangsege <kristian.spangsege at gmail dot com> 2013-02-22 18:04:41 UTC ---
(In reply to comment #4)
> I think 13.3 [over.match] ¶2 and 13.3.1 [over.match.funcs] ¶7  say the function
> template specialization must be generated before the number of arguments is
> checked to see if the candidate function is viable.

Not so.

Function templates must not be instantiated unless the number of arguments
match.

[C++11: 13.3/2]:

- First, a subset of the candidate functions (those that have the proper number
of arguments and meet certain other conditions) is selected to form a set of
viable functions (13.3.2).

- Then the best viable function is selected based on the implicit conversion
sequences (13.3.3.1) needed to match each argument to the corresponding
parameter of each viable function.

[C++11: 13.3.1/7]:

In each case where a candidate is a function template, candidate function
template specializations are generated using template argument deduction
(14.8.3, 14.8.2). Those candidates are then handled as candidate functions in
the usual way.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (4 preceding siblings ...)
  2013-02-22 18:05 ` kristian.spangsege at gmail dot com
@ 2013-02-22 18:48 ` redi at gcc dot gnu.org
  2013-02-22 22:01 ` daniel.kruegler at googlemail dot com
                   ` (10 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2013-02-22 18:48 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-02-22 18:47:55 UTC ---
(In reply to comment #5)
> Function templates must not be instantiated unless the number of arguments
> match.

Where does the standard say that?


> [C++11: 13.3/2]:
> 
> - First, a subset of the candidate functions (those that have the proper number
> of arguments and meet certain other conditions) is selected to form a set of
> viable functions (13.3.2).

You've quoted this out of context, the preceding sentence says "But, once the
candidate functions and argument lists have been identified, the selection of
the best function is the same in all cases:" 

i.e. identifying the candidate functions happens before considering the number
of arguments.

13.3.1 describes how the set of candidate functions (including function
template specializations) is found, then 13.3.2 describes how the number of
arguments is considered in order to find the subset of candidate functions that
are viable.

Also see 14.8.3 [temp.over]/1 "The complete set of candidate functions includes
all the synthesized declarations and all of the non-template overloaded
functions of the same name."

"The complete set" is the set *before* the number of arguments is considered to
find the viable subset. To synthesize the declaration Foo<int>::type must be
known, and that type results in an error.

That's how I read it anyway, I guess I'll leave this for someone else to
interpret.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (5 preceding siblings ...)
  2013-02-22 18:48 ` redi at gcc dot gnu.org
@ 2013-02-22 22:01 ` daniel.kruegler at googlemail dot com
  2013-02-22 22:17 ` redi at gcc dot gnu.org
                   ` (9 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2013-02-22 22:01 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #7 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2013-02-22 22:01:18 UTC ---
(In reply to comment #4)
> I think 13.3 [over.match] ¶2 and 13.3.1 [over.match.funcs] ¶7  say the function
> template specialization must be generated before the number of arguments is
> checked to see if the candidate function is viable.

After a cross-check with the core language group I completely agree with
Jonathan and exactly because of the paragraphs he quoted. Further-on, the
example is ill-formed, but no diagnostics required, as of [temp.inst]p6: 

"If the overload resolution process can determine the correct function to call
without instantiating a class template definition, it is unspecified whether
that instantiation actually takes place."

This sentence explains the implementation divergence (Thanks to Richard Smith
for reminding me to this part).


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (6 preceding siblings ...)
  2013-02-22 22:01 ` daniel.kruegler at googlemail dot com
@ 2013-02-22 22:17 ` redi at gcc dot gnu.org
  2013-02-22 23:10 ` kristian.spangsege at gmail dot com
                   ` (8 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2013-02-22 22:17 UTC (permalink / raw)
  To: gcc-bugs


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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-02-22 22:17:06 UTC ---
Thanks, Daniel.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (7 preceding siblings ...)
  2013-02-22 22:17 ` redi at gcc dot gnu.org
@ 2013-02-22 23:10 ` kristian.spangsege at gmail dot com
  2013-02-22 23:11 ` kristian.spangsege at gmail dot com
                   ` (7 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: kristian.spangsege at gmail dot com @ 2013-02-22 23:10 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #9 from Kristian Spangsege <kristian.spangsege at gmail dot com> 2013-02-22 23:09:59 UTC ---
Daniell, would you do me the favour of explaining exactly why you think my
example is ill-formed?


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (8 preceding siblings ...)
  2013-02-22 23:10 ` kristian.spangsege at gmail dot com
@ 2013-02-22 23:11 ` kristian.spangsege at gmail dot com
  2013-02-22 23:20 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: kristian.spangsege at gmail dot com @ 2013-02-22 23:11 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #10 from Kristian Spangsege <kristian.spangsege at gmail dot com> 2013-02-22 23:11:13 UTC ---
Daniel - excuse me :-)


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (9 preceding siblings ...)
  2013-02-22 23:11 ` kristian.spangsege at gmail dot com
@ 2013-02-22 23:20 ` redi at gcc dot gnu.org
  2013-02-22 23:39 ` kristian.spangsege at gmail dot com
                   ` (5 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2013-02-22 23:20 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-02-22 23:20:28 UTC ---
as explained in comment 6


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (10 preceding siblings ...)
  2013-02-22 23:20 ` redi at gcc dot gnu.org
@ 2013-02-22 23:39 ` kristian.spangsege at gmail dot com
  2013-02-22 23:46 ` kristian.spangsege at gmail dot com
                   ` (4 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: kristian.spangsege at gmail dot com @ 2013-02-22 23:39 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #12 from Kristian Spangsege <kristian.spangsege at gmail dot com> 2013-02-22 23:38:47 UTC ---
Yes, so far so good, but where does the standard say that if synthesis fails
for a particular candidate function template declaration, during overload
resolution, then the code is ill-formed?


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (11 preceding siblings ...)
  2013-02-22 23:39 ` kristian.spangsege at gmail dot com
@ 2013-02-22 23:46 ` kristian.spangsege at gmail dot com
  2013-02-23  0:03 ` kristian.spangsege at gmail dot com
                   ` (3 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: kristian.spangsege at gmail dot com @ 2013-02-22 23:46 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #13 from Kristian Spangsege <kristian.spangsege at gmail dot com> 2013-02-22 23:46:04 UTC ---
Jonathan,

"The complete set" is the set *before* the number of arguments is considered to
find the viable subset. To synthesize the declaration Foo<int>::type must be
known, and that type results in an error.

Why then is the following accepted (replaced "Foo<S>::type" with "S::type"):

template<class S> struct Foo {
  typedef typename S::type type;
};

template<class> void foo();
template<class S> typename S::type foo(int);

int main()
{
  foo<int>();
}


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (12 preceding siblings ...)
  2013-02-22 23:46 ` kristian.spangsege at gmail dot com
@ 2013-02-23  0:03 ` kristian.spangsege at gmail dot com
  2013-02-23  0:29 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: kristian.spangsege at gmail dot com @ 2013-02-23  0:03 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #14 from Kristian Spangsege <kristian.spangsege at gmail dot com> 2013-02-23 00:02:55 UTC ---
Jonathan, you say that:

"Foo::type exists unconditionally, so SFINAE doesn't apply.
The invalid type is not in the immediate context of the substitution."

Could you point me to the relevant section(s) in the standard?


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (13 preceding siblings ...)
  2013-02-23  0:03 ` kristian.spangsege at gmail dot com
@ 2013-02-23  0:29 ` redi at gcc dot gnu.org
  2013-02-23  0:31 ` redi at gcc dot gnu.org
  2013-02-23  1:35 ` kristian.spangsege at gmail dot com
  16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2013-02-23  0:29 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #15 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-02-23 00:29:17 UTC ---
[temp.deduct]/8

This is not the right place to learn C++, please use the gcc-help mailing list
or another site.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (14 preceding siblings ...)
  2013-02-23  0:29 ` redi at gcc dot gnu.org
@ 2013-02-23  0:31 ` redi at gcc dot gnu.org
  2013-02-23  1:35 ` kristian.spangsege at gmail dot com
  16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2013-02-23  0:31 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #16 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-02-23 00:31:00 UTC ---
Or see http://isocpp.org/forums


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Bug c++/56421] Non-matching overload produces template substitution error
  2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
                   ` (15 preceding siblings ...)
  2013-02-23  0:31 ` redi at gcc dot gnu.org
@ 2013-02-23  1:35 ` kristian.spangsege at gmail dot com
  16 siblings, 0 replies; 18+ messages in thread
From: kristian.spangsege at gmail dot com @ 2013-02-23  1:35 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #17 from Kristian Spangsege <kristian.spangsege at gmail dot com> 2013-02-23 01:35:02 UTC ---
Dude, you are insulting my ego! :-)

Anyway, thanks for pointing me to [temp.deduct]/8. SFINAE certainly does not
apply in my example.


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2013-02-23  1:35 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-21 20:02 [Bug c++/56421] New: Non-matching overload produces template substitution error kristian.spangsege at gmail dot com
2013-02-22  9:09 ` [Bug c++/56421] " mpolacek at gcc dot gnu.org
2013-02-22 10:42 ` redi at gcc dot gnu.org
2013-02-22 10:59 ` daniel.kruegler at googlemail dot com
2013-02-22 16:25 ` redi at gcc dot gnu.org
2013-02-22 18:05 ` kristian.spangsege at gmail dot com
2013-02-22 18:48 ` redi at gcc dot gnu.org
2013-02-22 22:01 ` daniel.kruegler at googlemail dot com
2013-02-22 22:17 ` redi at gcc dot gnu.org
2013-02-22 23:10 ` kristian.spangsege at gmail dot com
2013-02-22 23:11 ` kristian.spangsege at gmail dot com
2013-02-22 23:20 ` redi at gcc dot gnu.org
2013-02-22 23:39 ` kristian.spangsege at gmail dot com
2013-02-22 23:46 ` kristian.spangsege at gmail dot com
2013-02-23  0:03 ` kristian.spangsege at gmail dot com
2013-02-23  0:29 ` redi at gcc dot gnu.org
2013-02-23  0:31 ` redi at gcc dot gnu.org
2013-02-23  1:35 ` kristian.spangsege at gmail dot com

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).