#include <iostream>
#include <string>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
struct foo
{
foo(std::string& str1, std::string& str2) : _str1(str1), _str2(str2) {}
~foo() { std::cout << "Either \"" << _str1 << "\" or \"" << _str2 << "\" was returned." << std::endl; }
std::string& _str1;
std::string& _str2;
};
std::string foobar()
{
std::string str1("Hello, World!");
std::string str2("Goodbye, cruel World.");
foo f(str1, str2);
srand(time(NULL));
if (rand() % 2)
{
return str1;
}
return str2;
}
int main()
{
std::cout << "\"" << foobar() << "\" was actually returned." << std::endl;
return EXIT_SUCCESS;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8c3RkbGliLmg+IC8qIHNyYW5kLCByYW5kICovCiNpbmNsdWRlIDx0aW1lLmg+IC8qIHRpbWUgKi8KCnN0cnVjdCBmb28KewogICAgZm9vKHN0ZDo6c3RyaW5nJiBzdHIxLCBzdGQ6OnN0cmluZyYgc3RyMikgOiBfc3RyMShzdHIxKSwgX3N0cjIoc3RyMikge30KCiAgICB+Zm9vKCkgeyBzdGQ6OmNvdXQgPDwgIkVpdGhlciBcIiIgPDwgX3N0cjEgPDwgIlwiIG9yIFwiIiA8PCBfc3RyMiA8PCAiXCIgd2FzIHJldHVybmVkLiIgPDwgc3RkOjplbmRsOyB9CgogICAgc3RkOjpzdHJpbmcmIF9zdHIxOwogICAgc3RkOjpzdHJpbmcmIF9zdHIyOwp9OwoKc3RkOjpzdHJpbmcgZm9vYmFyKCkKewogICAgc3RkOjpzdHJpbmcgc3RyMSgiSGVsbG8sIFdvcmxkISIpOwogICAgc3RkOjpzdHJpbmcgc3RyMigiR29vZGJ5ZSwgY3J1ZWwgV29ybGQuIik7CiAgICBmb28gZihzdHIxLCBzdHIyKTsKCiAgICBzcmFuZCh0aW1lKE5VTEwpKTsKCiAgICBpZiAocmFuZCgpICUgMikKICAgIHsKICAgICAgICByZXR1cm4gc3RyMTsKICAgIH0KICAgIAogICAgcmV0dXJuIHN0cjI7Cn0KCmludCBtYWluKCkKewogICAgc3RkOjpjb3V0IDw8ICJcIiIgPDwgZm9vYmFyKCkgPDwgIlwiIHdhcyBhY3R1YWxseSByZXR1cm5lZC4iIDw8IHN0ZDo6ZW5kbDsKCiAgICByZXR1cm4gRVhJVF9TVUNDRVNTOwp9