fork(4) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. template<class T>
  5. struct Foo {
  6.  
  7. Foo(T val) {
  8. std::cout << "Called single argument ctor" << std::endl;
  9. // [...]
  10. }
  11.  
  12. // How to enforce U to be the same type as T?
  13. template<class... U>
  14. Foo(T first, U... vals) {
  15. std::cout << "Called multiple argument ctor" << std::endl;
  16. // [...]
  17. }
  18.  
  19. };
  20.  
  21. int main()
  22. {
  23.  
  24. // Should work as expected.
  25. Foo<int> single(1);
  26.  
  27. // Should work as expected.
  28. Foo<int> multiple(1,2,3,4,5);
  29.  
  30. // Should't work. The strings are not integers.
  31. Foo<int> mixedtype(1, "a", "b", "c");
  32.  
  33. // Also shouldn't work.
  34. // Foo<int> alsomixedtype(1, 1, "b", "c");
  35. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
Called single argument ctor
Called multiple argument ctor
Called multiple argument ctor