lundi 27 avril 2015

std::is_assignable and const pointer object

I have a small problem when trying to deduce things with std::is_assignable

My code:

#include <string>
#include <type_traits>

class Object{};

enum my_conv {
  string, const_object, object
};

template<typename T, typename V = void>
struct deducer;

template<typename T>
struct deducer<T, typename std::enable_if< std::is_constructible<std::string, T>::value >::type > {
  static const my_conv value = my_conv::string;
}; // (1) test for string                                                                                              

template<typename T>
struct deducer<T, typename std::enable_if< std::is_assignable<Object*&, T>::value >::type > {
  static const my_conv value = my_conv::object;
}; // (2) test for Object derived                                                                                      

template<typename T>
struct deducer<const T, typename std::enable_if< std::is_assignable<Object*&, T>::value >::type > {
  static const my_conv value = my_conv::const_object;
}; // (3) should test for const Object derived                                                                         

class Test : public Object {
public:
  Test() = default;
};

int main() {
  std::string   str;
  Test*         t = new Test;
  const Test*   tconst = static_cast<const Test*>(t);

  deducer<decltype(t)>::value;// deduce as (1)                                                                         
  deducer<decltype(str)>::value;//deduce as (2)                                                                        
  deducer<decltype(tconst)>::value;//fail to deduce as (3)... why?                                                     
}

And I really don't understand why the compiler fail to instantiate the third deducer....

Edit:

When testing I have seen that writing that:

struct deducer<const T*, typename std::enable_if< std::is_assignable<Object*&, T*>::value >::type >

make it work, but I think I still need some explanation... because I still don't understand what is wrong in the first time....

Aucun commentaire:

Enregistrer un commentaire