Facebook
From aaaaaaaaaaaa, 2 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 63
  1. //CastString.h
  2.  
  3. #ifndef __CAST_STRING_H
  4. #define __CAST_STRING_H
  5.  
  6. #include <string>
  7.  
  8. namespace Strings
  9. {
  10.  
  11. template<typename T>
  12. class CastString
  13. {
  14. public:
  15.     CastString();
  16.  
  17.     T cast(const std::string& value);
  18. };
  19.  
  20. void test(int i);
  21.  
  22. }
  23.  
  24. #endif
  25.  
  26. // CastString.cpp
  27.  
  28. #include "CastString.h"
  29.  
  30. namespace Strings
  31. {
  32.  
  33. template<>
  34. class CastString <int>
  35. {
  36. public:
  37.     CastString() {}
  38.  
  39.     int cast(const std::string& value){return std::stoi(value);}
  40. };
  41.  
  42. template<>
  43. class CastString <std::string>
  44. {
  45. public:
  46.     CastString() {}
  47.  
  48.     std::string cast(const std::string& value){return value;}
  49. };
  50.  
  51. void test(int i)
  52. {
  53.     // dziaĹ‚a dopiero po odkomentowaniu tego
  54.     // CastString<int>().cast("1234");
  55.     // CastString<std::string>().cast("1234");
  56. }
  57.  
  58. } // namespace Strings
  59.  

Replies to template specialization rss

Title Name Language When
Re: template specialization aaaaaaaaaaaa cpp 2 Months ago.