This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

std::string and literals


If I have the following:

void function(const std::string &x)
{
   cout << x;
}

int main()
{
   function("abcd");
}


then a temporary std::string will be constructed, internal memory will be 
allocated and the string "abcd" will be copied.

Which is obviously not the most efficient...

Q. Is there a way to specifically create a std::string instance that will 
just be a reference to the given literal? (with copy on write semantics 
of course).

e.g.

int main()
{
   function( std::string::literal("abcd"));   //hypothetical named constructor
}


or better yet, have the compiler figure this out automatically:

e.g.

namespace std
{
	class basic_string...
	{
		basic_string(readonly char *x)	// hypothetical "readonly" keyword
		{
			// just make a reference to x here
		}
	};
};

the hypothetical "readonly" is kind of like a more restrictive version of 
"const", basically meaning that no-one will ever modify it..


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]