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; }
Confirmed.
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.
(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; }
(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.
(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.
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.
Fixed already in 4.2.0.
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.