This is the mail archive of the gcc-prs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

c++/5296: Pointers to functions and template functions behave differentl



>Number:         5296
>Category:       c++
>Synopsis:       Pointers to functions and template functions behave differentl
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Mon Jan 07 02:16:01 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Wolfgang Bangerth
>Release:        unknown-1.0
>Organization:
>Environment:
2.95.2 and 3.0.1 and SS20011107
checked on Sun and Linux
>Description:
Pointers to regular functions and to template functions behave
differently. A simple example to show this is this:

	     #include <iostream>
	     #include <typeinfo>

	     template <typename T>  void print_type (const T &) {
	       std::cout << typeid(T).name() << std::endl;
	     };

	     /* no template */      void pp1 (int) {};
	     template <typename X>  void pp2 (X)   {};

	     int main () {
	       print_type (&pp1);
	       print_type (&pp2<int>);
	     };

We try to print the data types (mangled) of a pointer to a function
and a pointer to a template function. With gcc2.95.2, this example is
compilable and yields as output:

	   examples/step-1> c++ b.cc
	   examples/step-1> ./a.out
--->	   PFi_v
--->	   Fi_v
	   examples/step-1> c++ -v
	   Reading specs from /usr/lib/gcc-lib/i486-suse-linux/2.95.2/specs
	   gcc version 2.95.2 19991024 (release)       

The two lines of output should be identical, but are not. With
gcc3.0.1 and SS20011107, the example is not compilable:

	   examples/step-1> ~/bin/gcc-3.0.1/bin/c++ b.cc
	   b.cc: In function `int main()':
	   b.cc:13: no matching function for call to `print_type(<unknown type>)'           
	   examples/step-1> ~/bin/gcc-3.0.1/bin/c++ -v
	   Reading specs from /home/wolf/bin/gcc-3.0.1/lib/gcc-lib/i686-pc-linux-gnu/3.0.1/specs
	   Configured with: ../gcc-3.0.1/configure --prefix=/home/wolf/bin/gcc-3.0.1
	   Thread model: single
	   gcc version 3.0.1

Note that for both gcc versions, the problem disappears if we let
print_type take an argument of type `T' rather than `const T &'
(very strange).

The effect shown above results in the non-compilability of the
following example program:

	  template <typename T> struct Capsule {  T t;  };

	  template <typename T> void encapsulate (const T &) { Capsule<T> c; };

	  /* no template */      void pp1 (int);
	  template <typename X>  void pp2 (X);

	  void foo() {
	    encapsulate (&pp1);         // ok
	    encapsulate (&pp2<int>);    // fails!?!
	  };

With gcc2.95.2:
          examples/step-1> c++ -c a.cc
	  a.cc: In instantiation of `Capsule<void ()(int)>':
	  a.cc:3:   instantiated from `encapsulate<void ()(int)>(void (&)(int))'
	  a.cc:10:   instantiated from here
	  a.cc:1: field `Capsule<void ()(int)>::t' invalidly declared function type  
                                 ^^^^^^^^^^^^
Note that the template type of the Capsule class for the second
(template function) is a reference to a function rather than a
pointer. With gcc3.0.1 and SS20011107, the error message is again
rather unhelpful: 
	  examples/step-1> ~/bin/gcc-3.0.1/bin/c++ -c a.cc
	  a.cc: In function `void foo()':
	  a.cc:10: no matching function for call to `encapsulate(<unknown type>)'   
>How-To-Repeat:

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]