fork download
  1. #include <iostream>
  2. #include <type_traits>
  3. using namespace std;
  4.  
  5. struct alfa {};
  6.  
  7. struct foo { foo() { cout << "foo(void)" << endl; }};
  8.  
  9. struct bar { explicit bar(alfa&) { cout << "bar(alfa&)" << endl; } };
  10.  
  11. template<class T> struct with_allocator
  12. {
  13. struct unspecified_type;
  14.  
  15. T var;
  16. T& operator*() { return var; }
  17. T const& operator*() const { return var; }
  18.  
  19. template<class A>
  20. explicit with_allocator(A& a, typename enable_if< is_constructible<T,A&>::value, unspecified_type* >::type _ = nullptr)
  21. : var(a)
  22. { cout << "with allocator" << endl; }
  23.  
  24. template<class A>
  25. explicit with_allocator(A& a, typename enable_if<!is_constructible<T,A&>::value, unspecified_type* >::type _ = nullptr)
  26. { cout << "without allocator" << endl; }
  27. };
  28.  
  29. void run(foo&, bar&) { cout << "run(foo,bar)" << endl; }
  30.  
  31. int main() {
  32. // your code goes here
  33. alfa a;
  34.  
  35. with_allocator<foo> f (a);
  36. with_allocator<bar> b (a);
  37.  
  38. run(*f, *b);
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
foo(void)
without allocator
bar(alfa&)
with allocator
run(foo,bar)