Bug 29927 - template instantiation with function type
Summary: template instantiation with function type
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2006-11-21 16:06 UTC by s.nakayama
Modified: 2007-09-24 23:33 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.2.0
Known to fail:
Last reconfirmed: 2006-11-21 17:07:12


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description s.nakayama 2006-11-21 16:06:09 UTC
Compile the following code and run it. It cause a segmentation fault.

template <class T>
void foo()
{
  T bar;
  bar();
}

int main()
{ 
  foo<int ()>();
  return 0;
}
Comment 1 Andrew Pinski 2006-11-21 17:07:12 UTC
Confirmed.
Comment 2 Wolfgang Bangerth 2006-11-22 03:59:48 UTC
What exactly do you expect the code to do? 
  foo<int ()>();
leads to an instantiation of foo<T> with 
  T= int()()
i.e. reference to "no-arg function returning int". From
thereon I am a bit confused what exactly you intend to
do in foo()...

W.
Comment 3 s.nakayama 2006-11-22 14:57:12 UTC
(In reply to comment #2)
(In reply to comment #2)
> What exactly do you expect the code to do? 
>   foo<int ()>();
> leads to an instantiation of foo<T> with 
>   T= int()()
> i.e. reference to "no-arg function returning int". From
> thereon I am a bit confused what exactly you intend to
> do in foo()...

I expected that the compiler reject it. 
But Comeau compiler also accepted this code.
gcc 2.95.3 don't generate code that causes segmentation fault.(LINK error)
another test case:
following invalid code causes ICE.

template <class T>
void foo()
{
  T bar = 0;
  bar();
}

int main()
{ 
  foo<int ()>();
  return 0;
}
Comment 4 Wolfgang Bangerth 2006-11-22 15:31:55 UTC
(In reply to comment #3)
> I expected that the compiler reject it. 
> But Comeau compiler also accepted this code.

As does icc.

I can't see why the code would be invalid if one accepts that
  T bar;
is the declaration of a function pointer 'bar'. In that case, you
simply have an invalid function pointer and calling it should yield
a segfault, just as you get.

Now, here's a different interpretation that icc actually takes: it says
that
  T bar;
is the declaration for a function with name and signature
  int bar();
and the code will yield a linker error when compiled.

In any case, can you clarify why exactly you think the code should be
rejected?

W.
Comment 5 s.nakayama 2006-11-23 14:34:57 UTC
(In reply to comment #4)
> In any case, can you clarify why exactly you think the code should be
> rejected?


ISO 14882:2003 14.3.1 p3

>  If a declaration acquires a function type through a type dependent on a
>  template-parameter and this causes a declaration that does not use the
>  syntactic form of a function declarator to have function type, the
>  program is ill-formed.
Comment 6 Wolfgang Bangerth 2006-11-27 02:00:59 UTC
Excellent, this is exactly the quote that settles this. For 
reference, 14.3.1/3 comes with a (as usual non-normative) example:

  [Example:

     template<class T> struct A {
             static T t;
     };
     typedef int function();
     A<function> a;                  // ill-formed: would declare
                                     // A<function>::t
                                     // as a static member function

   --end example]

So both gcc and icc are wrong.

W.
Comment 7 s.nakayama 2007-05-17 06:04:44 UTC
Fixed already in 4.2.0.
Comment 8 Paolo Carlini 2007-09-24 23:33:10 UTC
Therefore, if I understand correctly, we want to reject the code and 4.2.0 was already implementing that behavior. This is not a regression, we can close it as fixed. If I'm mistaken, please reopen, thanks.