Hello, what I would like to implement, is that a template that wraps another type T would delegate a cast operator to another type Target to T if it exists in T. Example attached, showing something moderately simple as an example, and most of the body is to try assertions which confirm that SFINAE for these types work in general. The failing line works if the cast is of the form template <class Target> operator Target () const { return *get(); }; // or not const ... but of course fails for other types if T is not castable to Target, failing in a bad way, usually when compiler finds the cast ambiguous before knowing it actually wouldn't work because T is not castable to Target. This way the cast operator actually matches any type, so it is not very useful. Is there any other way to use SFINAE for the cast operator? I tried putting the invalid type in various places, but partial member template specialization is not allowed, cast operator must take void (not even a default) and so on. (tried these with 4.4.1 version I think, but I don't expect such a change between 4.4.1 and 4.4.3). $ g++ -v Using built-in specs. Target: x86_64-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_32=i586 --build=x86_64-redhat-linux Thread model: posix gcc version 4.4.1 20090725 (Red Hat 4.4.1-2) (GCC) $ g++ -Wall -c cast_wrapped.cpp cast_wrapped.cpp: In function ‘int main(int, char**)’: cast_wrapped.cpp:141: error: conversion from ‘WrappedPtr<CastableToString>’ to non-scalar type ‘std::string’ requested Or another version: $ ~/soft/gcc/exec/bin/g++ -v Using built-in specs. Target: x86_64-unknown-linux-gnu Configured with: ../../src/gcc-4.4.3/configure --prefix=/home/u2/soft/gcc/exec --enable-libssp --with-mpfr=/home/u2/soft/gcc/exec --with-gmp=/home/u2/soft/gcc/exec --with-cloog=/home/u2/soft/gcc/exec --with-ppl=/home/u2/soft/gcc/exec Thread model: posix gcc version 4.4.3 (GCC) $ ~/soft/gcc/exec/bin/g++ -Wall -c cast_wrapped.cpp cast_wrapped.cpp: In function ‘int main(int, char**)’: cast_wrapped.cpp:141: error: conversion from ‘WrappedPtr<CastableToString>’ to non-scalar type ‘std::string’ requested Edek
Attachment:
cast_wrapped.cpp
Description: Binary data