fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <type_traits>
  4. using namespace std;
  5.  
  6.  
  7. template<typename In>
  8. auto foo(In input, std::true_type) {
  9. return input[0];
  10. }
  11. template<typename In>
  12. auto foo(In input, std::false_type) {
  13. return input;
  14. }
  15. template<bool IsVector, typename In>
  16. auto foo(In input) {
  17. using tag = typename conditional<IsVector, true_type, false_type>::type;
  18. return foo(input, tag{});
  19. }
  20.  
  21.  
  22. int main() {
  23. cout << foo<true>(vector<double>{{1, 2 , 3}}) << endl;
  24. cout << foo<false>(4.5) << endl;
  25. return 0;
  26. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
1
4.5