public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "hniksic at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/39018]  New: Cannot take address of template function
Date: Thu, 29 Jan 2009 13:25:00 -0000	[thread overview]
Message-ID: <bug-39018-17243@http.gcc.gnu.org/bugzilla/> (raw)

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


             reply	other threads:[~2009-01-29 13:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-29 13:25 hniksic at gmail dot com [this message]
2009-01-30  0:21 ` [Bug c++/39018] " pinskia at gcc dot gnu dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-39018-17243@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).