If you have the following function:
static const map<ushort, ulong> MakeMap()
{
map<ushort, ulong> mymap;
for(int i=0; i<myTableSize; i++)
{
mymap[myTable[i].x] = myTable[i].y;
}
return mymap;
}
and somewhere use you have:
static const map<ushort, ulong> numMap = MakeMap();
will the compiler actually set numMap to the address returned from MakeMap or will it actually make a copy of the map? Also, is this even safe to do?
Thanks for your feedback!
The return type is not
static const map<ushort, ulong>. It’s onlyconst map<ushort, ulong>. The keywordstaticapplies to the function. That is, it’s the function which is static which means the function has internal linkage* and cannot be called from other translation unit*.* Go through the links to know about them.
Now coming back to your question, first of all,
constin the return type doesn’t make sense. The following is better:And then you can still write:
If you’re using good compiler, then most likely it will optimize on the return value. Read about: