Bug 39018 - Cannot take address of template function
Summary: Cannot take address of template function
Status: RESOLVED DUPLICATE of bug 11407
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.2
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-01-29 13:24 UTC by Hrvoje Niksic
Modified: 2009-01-30 00:21 UTC (History)
11 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hrvoje Niksic 2009-01-29 13:24:57 UTC
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)
Comment 1 Andrew Pinski 2009-01-30 00:21:31 UTC
Yes this is a known bug, please see PR 11407.

*** This bug has been marked as a duplicate of 11407 ***