Domanda

I was going over some slides of Scott Meyers and it had the following code sample

typedef std::vector<T> TVec;
TVec createTVec();  // factory function
TVec vt;
… 
vt = createTVec();  // in C++98, copy return value to - vt, then destroy return value

I dont understand the following points and would appreciate it if someone could clarify this:

  • Is createTVec an object on the stack of type TVec ? If so what are () brackets next to it for ?

What does vt = createTVec(); Was it suppose to be vt = createTVec; ??

È stato utile?

Soluzione

It is a function, you have a declaration:

TVec createTVec();  // factory function

so it might look like:

TVec createTVec() {
   TVec ret;
   // initialize it
   return ret;
}

What does vt = createTVec();

it assigns result from createTVec() function to vt variable.

In C++98 this function will return vector using temporary, since C++11 move semantics will be used. But, actually compiler might (and probably will) in such case do (N)RVO - Return Value Optimization.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top