fork download
  1. //User types
  2. struct field_t{};
  3. struct other_t{};
  4. struct StructT{other_t field;field_t other;};
  5. //Implementation detail
  6. template<typename T,typename FIELD_T>
  7. struct type_has_field{
  8. struct Fallback {FIELD_T field;};
  9. struct Derived:T,Fallback{};
  10. template<typename C, C> struct ChT;
  11. template<typename C> static char (&f(ChT<FIELD_T Fallback::*, &C::field>*))[1];
  12.  
  13. static bool const value = sizeof(f<Derived>(0)) == 2;
  14. };
  15. //Experiment
  16.  
  17. static_assert(
  18. false==type_has_field<StructT,field_t>::value,
  19. "field_t StructT::field - should not exist"
  20. );
  21.  
  22. int main(){return 0;}
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘const bool type_has_field<StructT, field_t>::value’:
prog.cpp:18:43:   required from here
prog.cpp:13:37: error: no matching function for call to ‘type_has_field<StructT, field_t>::f(int)’
   static bool const value = sizeof(f<Derived>(0)) == 2;
                                     ^
prog.cpp:13:37: note: candidate is:
prog.cpp:11:38: note: template<class C> static char (& type_has_field<T, FIELD_T>::f(type_has_field<T, FIELD_T>::ChT<FIELD_T type_has_field<T, FIELD_T>::Fallback::*, (& C:: field)>*))[1] [with C = C; T = StructT; FIELD_T = field_t]
   template<typename C> static char (&f(ChT<FIELD_T Fallback::*, &C::field>*))[1];
                                      ^
prog.cpp:11:38: note:   template argument deduction/substitution failed:
prog.cpp: In substitution of ‘template<class C> static char (& type_has_field<T, FIELD_T>::f(type_has_field<T, FIELD_T>::ChT<FIELD_T type_has_field<T, FIELD_T>::Fallback::*, (& C:: field)>*))[1] [with C = C; T = StructT; FIELD_T = field_t] [with C = type_has_field<StructT, field_t>::Derived]’:
prog.cpp:13:37:   required from ‘const bool type_has_field<StructT, field_t>::value’
prog.cpp:18:43:   required from here
prog.cpp:11:65: error: reference to ‘type_has_field<StructT, field_t>::Derived::field’ is ambiguous
   template<typename C> static char (&f(ChT<FIELD_T Fallback::*, &C::field>*))[1];
                                                                 ^
prog.cpp:8:28: note: candidates are: field_t type_has_field<StructT, field_t>::Fallback::field
   struct Fallback {FIELD_T field;};
                            ^
prog.cpp:4:24: note:                 other_t StructT::field
 struct StructT{other_t field;field_t other;};
                        ^
stdout
Standard output is empty