fork download
  1. #include <type_traits>
  2.  
  3. template <class T>
  4. class forward_if_wrapper {
  5.  
  6. template <class U, class Enable = std::enable_if_t<std::is_lvalue_reference_v<U>>>
  7. static U forward(U&& u) {
  8. return u;
  9. }
  10.  
  11. template <class U, class Enable = std::enable_if_t<!std::is_lvalue_reference_v<U>>>
  12. static U&& forward(U&& t) {
  13. return static_cast<U&&>(t);
  14. }
  15. };
  16. auto forward = [](auto&& t) { return forward_if_wrapper<decltype(t)>::forward(t); };
  17. template <class T> auto forward_if = [](auto&& u) { return forward_if_wrapper<T>::forward(u); };
  18.  
  19. // --------
  20.  
  21. #include <stdio.h>
  22. #include <vector>
  23.  
  24. template<class Elt>
  25. void bar(Elt&& e) {
  26. printf("Called %s\n", __PRETTY_FUNCTION__);
  27. }
  28.  
  29. template<class Container>
  30. void foo(Container&& c) {
  31. for (auto&& elt : c) {
  32. bar(forward_if<Container>(elt));
  33. }
  34. }
  35.  
  36. int main() {
  37. std::vector<int> v = {1,2};
  38. foo(v);
  39. foo(std::move(v));
  40. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/4.9/type_traits:35:0,
                 from prog.cpp:1:
/usr/include/c++/4.9/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
prog.cpp:6:44: error: 'enable_if_t' in namespace 'std' does not name a template type
     template <class U, class Enable = std::enable_if_t<std::is_lvalue_reference_v<U>>>
                                            ^
prog.cpp:6:55: error: expected '>' before '<' token
     template <class U, class Enable = std::enable_if_t<std::is_lvalue_reference_v<U>>>
                                                       ^
prog.cpp:7:29: error: expected unqualified-id before '{' token
     static U forward(U&& u) {
                             ^
prog.cpp:11:44: error: 'enable_if_t' in namespace 'std' does not name a template type
     template <class U, class Enable = std::enable_if_t<!std::is_lvalue_reference_v<U>>>
                                            ^
prog.cpp:11:55: error: expected '>' before '<' token
     template <class U, class Enable = std::enable_if_t<!std::is_lvalue_reference_v<U>>>
                                                       ^
prog.cpp:12:31: error: expected unqualified-id before '{' token
     static U&& forward(U&& t) {
                               ^
prog.cpp:16:6: error: 'forward' does not name a type
 auto forward = [](auto&& t) { return forward_if_wrapper<decltype(t)>::forward(t); };
      ^
prog.cpp:17:25: error: 'forward_if' does not name a type
 template <class T> auto forward_if = [](auto&& u) { return forward_if_wrapper<T>::forward(u); };
                         ^
prog.cpp:25:13: error: expected ',' or '...' before '&&' token
 void bar(Elt&& e) {
             ^
prog.cpp:30:19: error: expected ',' or '...' before '&&' token
 void foo(Container&& c) {
                   ^
prog.cpp: In function 'void foo(Container)':
prog.cpp:31:14: error: expected unqualified-id before '&&' token
     for (auto&& elt : c) {
              ^
prog.cpp:31:14: error: expected ';' before '&&' token
prog.cpp:31:21: error: expected ';' before ':' token
     for (auto&& elt : c) {
                     ^
prog.cpp:31:21: error: expected primary-expression before ':' token
prog.cpp:31:21: error: expected ')' before ':' token
prog.cpp:31:21: error: expected primary-expression before ':' token
prog.cpp: In function 'int main()':
prog.cpp:37:30: error: in C++98 'v' must be initialized by constructor, not by '{...}'
     std::vector<int> v = {1,2};
                              ^
prog.cpp:39:9: error: 'move' is not a member of 'std'
     foo(std::move(v));
         ^
prog.cpp: In instantiation of 'void foo(Container) [with Container = std::vector<int>]':
prog.cpp:38:10:   required from here
prog.cpp:31:14: error: label 'elt' used but not defined
     for (auto&& elt : c) {
              ^
stdout
Standard output is empty