3

Suppose that I have the following code:

#include <iostream>
#include <map>
#include <string>

struct Message
{
  std::string to;
  std::string text;
};

void foo(const Message& msg)
{
  std::cout << msg.to << ' ' << msg.text << std::endl;
}

int main()
{
  std::map<std::string, std::string> dict;
  dict["bar"] = "baz";

  foo(Message{ dict["bar"], "foobar" });
  foo(Message{ "baz", "foobar" });

  Message msg;
  msg.to = dict["bar"];
  msg.text = "foobar";
  foo(msg);
}

I expect the output to be:

baz foobar
baz foobar
baz foobar

However, it seems the Visual Studio 2013 thinks another way:

 foobar
baz foobar
baz foobar

Why? What's the reason behind this? Is it a bug?

FrozenHeart
  • 18,778
  • 30
  • 107
  • 223

0 Answers0