fork(1) download
  1. #include <vector>
  2. #include <unordered_set>
  3. #include <algorithm>
  4. #include <functional>
  5.  
  6. template<typename V, typename... Args>
  7. void autoEmplace(V& vec, Args&& ...args)
  8. {
  9. return autoEmplace(typename std::is_pod<
  10. typename V::value_type>::type(), vec,
  11. std::forward<Args>(args)...);
  12. }
  13.  
  14. template<typename V, typename... Args>
  15. void autoEmplace(std::true_type, V& vec, Args&& ...args)
  16. {
  17. vec.push_back({std::forward<Args>(args)...});
  18. }
  19.  
  20. template<typename V, typename... Args>
  21. void autoEmplace(std::false_type, V& vec, Args&& ...args)
  22. {
  23. vec.emplace_back(std::forward<Args>(args)...);
  24. }
  25.  
  26. #include <iostream>
  27.  
  28. struct Foo {
  29. int i;
  30. short n;
  31. };
  32.  
  33. struct Bars {
  34. int i;
  35. double n;
  36. };
  37.  
  38. using Lol = std::unordered_set<int>;
  39.  
  40. int main() {
  41. Foo foo{42}; //fine
  42. std::vector<Foo> v;
  43. std::vector<Bars> b;
  44. std::vector<Lol> h;
  45. autoEmplace(v, 42);
  46. autoEmplace(b, 42, 3.14159265358979323);
  47. autoEmplace(h, 10, std::hash<int>(), std::equal_to<int>());
  48. std::cout << v.back().i << std::endl;
  49. std::cout << b.back().n << std::endl;
  50. }
Compilation error #stdin compilation error #stdout 0s 3460KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of 'void autoEmplace(V&, Args&& ...) [with V = std::vector<std::unordered_set<int> >; Args = {int, std::hash<int>, std::equal_to<int>}]':
prog.cpp:47:59:   required from here
prog.cpp:9:20: error: invalid initialization of non-const reference of type 'std::integral_constant<bool, false>&' from an rvalue of type 'std::integral_constant<bool, false>::type {aka std::integral_constant<bool, false>}'
  return autoEmplace(typename std::is_pod<
                    ^
prog.cpp:7:6: note:   initializing argument 1 of 'void autoEmplace(V&, Args&& ...) [with V = std::integral_constant<bool, false>; Args = {std::vector<std::unordered_set<int, std::hash<int>, std::equal_to<int>, std::allocator<int> >, std::allocator<std::unordered_set<int, std::hash<int>, std::equal_to<int>, std::allocator<int> > > >&, int, std::hash<int>, std::equal_to<int>}]'
 void autoEmplace(V& vec, Args&& ...args)
      ^
prog.cpp:11:30: error: return-statement with a value, in function returning 'void' [-fpermissive]
   std::forward<Args>(args)...);
                              ^
stdout
Standard output is empty