fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class T>
  5. struct Foo{
  6. // template<class S,typename = std::enable_if_t<std::is_convertible<S, T>::value>>
  7. //template<class S>
  8. template<class S,typename = std::enable_if_t<std::is_convertible<S*, T*>::value>>
  9.  
  10. Foo(S* s):_t(s){}
  11. T * _t;
  12. };
  13.  
  14. struct Bar{};
  15.  
  16. struct Bar2 : public Bar {};
  17.  
  18. struct NotBar{};
  19.  
  20. void do_something(Foo<Bar> f){
  21. cout<<"In Foo do something"<<endl;
  22. }
  23.  
  24. void do_something(std::string f){
  25. cout<<"In string do something"<<endl;
  26.  
  27. }
  28.  
  29. int main() {
  30. const char * string = "string";
  31. do_something(string);
  32. Bar2 bar2;
  33. do_something(&bar2);
  34. Foo<Bar> fb(&bar2);
  35. NotBar nb;
  36. // this SHOULD fail
  37. //Foo<Bar> fb2(&nb);
  38. return 0;
  39. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
In string do something
In Foo do something