c++ - SFINAE canAdd template problem -


i'm trying tow write sfinae template determine whether 2 classes can added together. better understand how sfinae works, rather particular "real world" reason.

so i've come is

#include <assert.h>  struct vec {   vec operator+(vec v ); };  template<typename t1, typename t2> struct canbeadded {   struct 1 { char _[1]; };   struct 2 { char _[2]; };    template<typename w>   static w make();    template<int i>   struct force_int { typedef void* t; };     static 1 test_sfinae( typename force_int< sizeof( make<t1>() + make<t2>() ) >::t );   static 2 test_sfinae( ... );    enum { value = sizeof( test_sfinae( null ) )==1 }; };   int main() {   assert((canbeadded<int, int>::value));   assert((canbeadded<int, char*>::value));   assert((canbeadded<char*, int>::value));   assert((canbeadded<vec, vec>::value));   assert((canbeadded<char*, int*>::value)); } 

this compiles except last line, gives

finae_test.cpp: in instantiation of ‘canbeadded<char*, int*>’: sfinae_test.cpp:76:   instantiated here sfinae_test.cpp:40: error: invalid operands of types ‘char*’ , ‘int*’ binary ‘operator+’ 

so error kind-of i'd expect, i'd expect compiler find test_sfinae( ... ) definition , use instead (and not complain 1 doesn't parse.

clearly i'm missing something, don't know is.

it looks me you've run problem that's discussed in core issue 339 n2634. bottom line you're pushing bit beyond compiler can handle, though you're doing allowed standard. c++ 0x add more detail , won't result in sfinae failure versus hard error. see n3000, §14.9.2, if want gory details.


Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -