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]

BUG (GCC-2.95.2): Default conversions not applied to template classes


I searched briefly to see if this has already been reported, but didn't
see anything.  If it's a duplicate, I apologize.

When using a fully-qualified template class in a non-template function, 
default conversions are not searched, resulting in a compiler error.

This has been observed with GCC-2.95.2 under RedHat Linux 6.1.

For example:

  // testing operator bool...
  template <class T>
    class foo
    {
  public:
    // default conversion to bool
    operator bool() const
      {return true;}
    };

  bool f (const foo<int>& fi)
    {
    if (fi) // apply default conversion
      return true;
    return false;
    }

  int main ()
    {
    foo<int> fi;
    f (fi);
    return 0;
    }

When I compile the above code with GCC-2.95.2:

  g++ opbool2.cpp

I get the following errors:

  opbool2.cpp: In function `bool f(const foo<int> &)':
  opbool2.cpp:13: `const class foo<int>' used where a `bool' was expected

If the function is a template function, e.g.

  template <class T>
    bool f (const foo<T>& fi)
    {
    if (fi)
      return true;
    return false;
    }

Or if ``operator bool()'' is explicitly referenced:

  ...
  if (fi.operator bool())
  ...

The code compiles fine.  The code also compiles with both a non-template class
and a non-template function.

Additional Information:

  g++ -dumpspecs:
    *asm:
    %{v:-V} %{Qy:} %{!Qn:-Qy} %{n} %{T} %{Ym,*} %{Yd,*} %{Wa,*:%*}

    *asm_final:
    %|

    *cpp:
    %(cpp_cpu) %{fPIC:-D__PIC__ -D__pic__} %{fpic:-D__PIC__ -D__pic__} %{posix:-D_POSIX_SOURCE} %{pthread:-D_REENTRANT}

    *cc1:
    %(cc1_cpu) %{profile:-p}

    *cc1plus:


    *endfile:
    %{!shared:crtend.o%s} %{shared:crtendS.o%s} crtn.o%s

    *link:
    -m elf_i386 %{shared:-shared}   %{!shared:     %{!ibcs:       %{!static: 	%{rdynamic:-export-dynamic} 	%{!dynamic-linker:-dynamic-linker /lib/ld-linux.so.2}} 	%{static:-static}}}

    *lib:
    %{shared: -lc}    %{!shared: %{mieee-fp:-lieee} %{pthread:-lpthread} 	%{profile:-lc_p} %{!profile: -lc}}

    *libgcc:
    -lgcc

    *startfile:
    %{!shared:      %{pg:gcrt1.o%s} %{!pg:%{p:gcrt1.o%s} 		       %{!p:%{profile:gcrt1.o%s} 			 %{!profile:crt1.o%s}}}}    crti.o%s %{!shared:crtbegin.o%s} %{shared:crtbeginS.o%s}

    *switches_need_spaces:


    *signed_char:
    %{funsigned-char:-D__CHAR_UNSIGNED__}

    *predefines:
    -D__ELF__ -Dunix -D__i386__ -Dlinux -Asystem(posix)

    *cross_compile:
    0

    *version:
    2.95.2

    *multilib:
    . ;

    *multilib_defaults:


    *multilib_extra:


    *multilib_matches:


    *linker:
    collect2

    *cpp_486:
    %{!ansi:-Di486} -D__i486 -D__i486__

    *cpp_586:
    %{!ansi:-Di586 -Dpentium} 	-D__i586 -D__i586__ -D__pentium -D__pentium__

    *cpp_k6:
    %{!ansi:-Di586 -Dk6} 	-D__i586 -D__i586__ -D__k6 -D__k6__

    *cpp_686:
    %{!ansi:-Di686 -Dpentiumpro} 	-D__i686 -D__i686__ -D__pentiumpro -D__pentiumpro__

    *cpp_cpu_default:
    %(cpp_686)

    *cpp_cpu:
    -Acpu(i386) -Amachine(i386) %{!ansi:-Di386} -D__i386 -D__i386__ %{mcpu=i486:%(cpp_486)} %{m486:%(cpp_486)} %{mpentium:%(cpp_586)} %{mcpu=pentium:%(cpp_586)} %{mpentiumpro:%(cpp_686)} %{mcpu=pentiumpro:%(cpp_686)} %{mcpu=k6:%(cpp_k6)} %{!mcpu*:%{!m486:%{!mpentium*:%(cpp_cpu_default)}}}

    *cc1_cpu:
    %{!mcpu*: %{m386:-mcpu=i386 -march=i386} %{m486:-mcpu=i486 -march=i486} %{mpentium:-mcpu=pentium} %{mpentiumpro:-mcpu=pentiumpro}}

    *link_command:
    %{!fsyntax-only:  %{!c:%{!M:%{!MM:%{!E:%{!S:%(linker) %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} 			%{r} %{s} %{t} %{u*} %{x} %{z} %{Z}			%{!A:%{!nostdlib:%{!nostartfiles:%S}}}			%{static:} %{L*} %D %o			%{!nostdlib:%{!nodefaultlibs:%G %L %G}}			%{!A:%{!nostdlib:%{!nostartfiles:%E}}}			%{T*}			
     }}}}}}


  g++ -dumpversion:
    2.95.2

  g++ -dumpmachine:
    i686-pc-linux-gnu

Thanks,

Jonathan Pryor
jonpryor@vt.edu



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