fork download
  1. #include <string>
  2. #include <type_traits>
  3.  
  4. class Object{};
  5.  
  6. enum my_conv {
  7. string, const_object, object
  8. };
  9.  
  10. template<typename T, typename V = void>
  11. struct deducer;
  12.  
  13. template<typename T>
  14. struct deducer<T, typename std::enable_if< std::is_constructible<std::string, T>::value >::type > {
  15. static const my_conv value = my_conv::string;
  16. }; // (1) test for string
  17.  
  18. template<typename T>
  19. struct deducer<T, typename std::enable_if< std::is_assignable<Object*&, T>::value >::type > {
  20. static const my_conv value = my_conv::object;
  21. }; // (2) test for Object derived
  22.  
  23. template<typename T>
  24. struct deducer<const T, typename std::enable_if< std::is_assignable<Object*&, T>::value >::type > {
  25. static const my_conv value = my_conv::const_object;
  26. }; // (3) should test for const Object derived
  27.  
  28. class Test : public Object {
  29. public:
  30. Test() = default;
  31. };
  32.  
  33. int main() {
  34. std::string str;
  35. Test* t = new Test;
  36. const Test* tconst = static_cast<const Test*>(t);
  37.  
  38. deducer<decltype(t)>::value;// deduce as (1)
  39. deducer<decltype(str)>::value;//deduce as (2)
  40. deducer<decltype(tconst)>::value;//fail to deduce as (1)... why?
  41. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:40:3: error: incomplete type 'deducer<const Test*>' used in nested name specifier
   deducer<decltype(tconst)>::value;//fail to deduce as (1)... why?                                                     
   ^
stdout
Standard output is empty