fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. struct A
  5. {
  6. A() { std::cout << "A:: default constructor\n" ; }
  7.  
  8. A( const char* aa ) : a(aa) { std::cout << "A:: one arg constructor\n" ; }
  9.  
  10. A ( const A& that ) : a(that.a) { std::cout << "A:: copy constructor\n" ; }
  11.  
  12. A& operator= ( const A& that )
  13. {
  14. std::cout << "A:: assignment\n" ;
  15. a = that. a ;
  16. return *this ;
  17. }
  18.  
  19. std::string a ;
  20. };
  21.  
  22. #include <map>
  23.  
  24. int main()
  25. {
  26. std::map< int, A > map ;
  27.  
  28. {
  29. // possibly only if A is DefaultConstructible
  30. map[1] = "hello world" ;
  31. /*
  32.   A:: one arg constructor
  33.   A:: default constructor
  34.   A:: assignment
  35.   */
  36. }
  37. std::cout << "------------------------\n" ;
  38. {
  39. // possibly only if A is CopyConstructible
  40. map.insert( { 2, "hello again" } ) ;
  41. /*
  42.   A:: one arg constructor
  43.   A:: copy constructor
  44.   */
  45. }
  46. std::cout << "------------------------\n" ;
  47. {
  48. map.emplace( 3, "hello for a third time" ) ;
  49. // A:: one arg constructor
  50. }
  51. }
  52.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
A:: one arg constructor
A:: default constructor
A:: assignment
------------------------
A:: one arg constructor
A:: copy constructor
------------------------
A:: one arg constructor