{}
run-icon
main.cpp
#include <stdio.h> #include <string> #include <vector> using namespace std; struct S { string name; S(const string & n) : name(n) { printf("%s(%s)\n", __func__, name.c_str()); } ~S() { printf("%s(%s)\n", __func__, name.c_str()); } void g() const { printf("%s(%s)\n", __func__, name.c_str()); } }; S f() { return S("f"); } S s("global"); int main(int argc, char ** argv) { (void)argc, (void)argv; printf("%s: begin\n", __func__); for (int i=0; i<2; i++) { const auto & z = i ? s : f(); z.g(); } printf("%s: end\n", __func__); }
Output