The parser does not reject invalid code, but instead passes to the compiler.
The compiler fails with different internal compiler errors depending on the
optimization level. With levels -O2, -O3 and -Os, the compiler generates valid
object code without complaining anything.

How to reproduce?

# cat ice.cc
struct A {
    inline void foo() {}
};

template<typename T, void (T::*fnc)()>
void call(){
    T t;
    (t.*fnc)();
}

template<typename T >
void deduce_type(void (T::*fnc)()){
    call<T,fnc>();
}

int main() {
    deduce_type(&A::foo);

    return 0;
}

# g++ ICE.cc -o ICE -Wall
ICE.cc: In function ‘void call() [with T = A, void (T::* fnc)() = fnc]’:
ICE.cc:8: internal compiler error: in expand_expr_real_1, at expr.c:7314

# g++ ICE.cc -o ICE -Wall -O1
ICE.cc: In function ‘void call() [with T = A, void (T::* fnc)() = fnc]’:
ICE.cc:6: internal compiler error: in make_decl_rtl, at varasm.c:1290

# g++ ICE.cc -o ICE -Wall -O2

If we use instead of a member function pointer a usual function pointer like in
the following code snippet

template<void (*f)()>
void call(){
    f();
}

template<typename F>
void deduce_type(void (*f)(), F){
    call<f>();
}

# g++ -c ICE2.cc -o ICE2.o -Wall
ICE2.cc: In function ‘void deduce_type(void (*)(), F)’:
ICE2.cc:24: error: ‘f’ cannot appear in a constant-expression
ICE2.cc:24: error: no matching function for call to ‘call()’

# g++ -v
Using built-in specs.
Target: i586-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla
--enable-bootstrap --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk
--disable-dssi --enable-plugin
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre
--enable-libgcj-multifile --enable-java-maintainer-mode
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib
--with-ppl --with-cloog --with-tune=generic --with-arch=i586
--build=i586-redhat-linux
Thread model: posix
gcc version 4.4.1 20090725 (Red Hat 4.4.1-2) (GCC)


-- 
           Summary: Parser does not recogize local variable in constant
                    expression for member function pointers
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mschulze at ivs dot cs dot ovgu dot de
 GCC build triplet: i586-redhat-linux
  GCC host triplet: i586-redhat-linux
GCC target triplet: i586-redhat-linux


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

Reply via email to