This is the mail archive of the gcc-bugs@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]

[Bug c++/39018] New: Cannot take address of template function


The following code fails to compile under g++ 4.3.2:

class Bar {};

template<int N>
class Foo {
  double val[N];
};

template<int N>
void fun(Foo<N>* ptr) {
}

typedef void (*T)(Bar*);

T funptr = (T) &fun<2>;

The error message is:

$ g++ -c a.cc
a.cc:14: error: address of overloaded function with no contextual type
information

I don't know if the standard allows this, but it looks like it should be
allowed to take the address of a templated function, because it works in other
contexts (see below).  It seems unambiguous because with foo<2> we a specific
variant of the function is requested.  Intel's C++ compiler (icpc versions 9.1
and 10.1) accepts it.

It is possible to work around this error by providing "contextual type
information", although how to do that is not immediately obvious to everyone. 
The workaround that worked for me is to replace the last line with:

typedef void (*U)(Foo<2>*);
T funptr = (T) (U) &fun<2>;

which compiles without errors or warnings.

The error is similar to bug 29143, but I don't think it's a dup.  In that case,
the "contextual type information" is present, but apparently ignored by the
compiler.  In this case, context doesn't help with type determination, but it
shouldn't be necessary since foo<2> uniquely identifies the function.

Detailed version information:

$ g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.2-1ubuntu11'
--with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3
--program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug
--enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release
--build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu11)


-- 
           Summary: Cannot take address of template function
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: hniksic at gmail dot com


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


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