template.cpp 972 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (C) 2009-2012 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0
  3. // (see accompanying file LICENSE_1_0.txt or a copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Home at http://www.boost.org/libs/utility/identity_type
  6. #include <boost/utility/identity_type.hpp>
  7. #include <map>
  8. #include <iostream>
  9. //[template_f_decl
  10. #define ARG(type, n) type arg ## n
  11. template<typename T>
  12. void f( // Prefix macro with `typename` in templates.
  13. ARG(typename BOOST_IDENTITY_TYPE((std::map<int, T>)), 1)
  14. ) {
  15. std::cout << arg1[0] << std::endl;
  16. }
  17. //]
  18. //[template_g_decl
  19. template<typename T>
  20. void g(
  21. std::map<int, T> arg1
  22. ) {
  23. std::cout << arg1[0] << std::endl;
  24. }
  25. //]
  26. int main() {
  27. //[template_f_call
  28. std::map<int, char> a;
  29. a[0] = 'a';
  30. f<char>(a); // OK...
  31. // f(a); // ... but error.
  32. //]
  33. //[template_g_call
  34. g<char>(a); // OK...
  35. g(a); // ... and also OK.
  36. //]
  37. return 0;
  38. }