fork download
  1. #include <stdio.h>
  2. #include <initializer_list>
  3. #include <utility>
  4.  
  5. struct A {
  6. void operator=(std::initializer_list<int> a){printf("int[%zu]:",a.size());for (auto x:a) printf(" %d",x); printf("\n");}
  7. void operator=(float a){printf("float: %.1f\n",a);}
  8. };
  9.  
  10. struct B : A {
  11. template <typename T>
  12. void _copy(T&& x)
  13. {
  14. A::operator=(x);
  15. printf("+ \n");
  16. }
  17.  
  18. template <typename T>
  19. void operator=(T x) {
  20. _copy(std::forward<T>(x));
  21. }
  22.  
  23. template <typename T>
  24. void operator=(std::initializer_list<T> x) {
  25. _copy(x);
  26. }
  27. };
  28.  
  29. int main() {
  30.  
  31. A a;
  32. a={1,2,5};
  33. a=5.6;
  34.  
  35. B b;
  36. b={1,5,6};
  37. b=4.7;
  38.  
  39. }
  40.  
Success #stdin #stdout 0s 4964KB
stdin
Standard input is empty
stdout
int[3]: 1 2 5
float: 5.6
int[3]: 1 5 6
+ 
float: 4.7
+