Why isn't string assignment optimised when the length is known to the compiler?

The first can't be optimised into the second. In the first, the length of the string is unknown and so has to be calculated, in the second you tell it how long it is, so no calculation is needed.

The first can't be optimised into the second. In the first, the length of the string is unknown and so has to be calculated, in the second you tell it how long it is, so no calculation is needed. And using sizeof() makes no difference - that is calculated at compile time too.

The constructor that the first case uses is: string( const char * s ); there is no way of this constructor detecting it is being given a string literal, much less calculating its length at compile time. Also, constructing strings from C-style string literals happens relatively rarely in real code - it simply isn't worth optimising. And if you do need to optimise it, simply re-write: while( BIGLOOP ) { string s( "foobar" ); ... } as: string f( "foobar" ); while( BIGLOOP ) { string s( f ); ... }.

3 The compiler should be able to figure out the length of the string at compile time in the same way it can figure out sizeof(...) since the string is constant – Greg Jan 31 '10 at 23:03 3 I think his point is valid, in the first case, the compiler does know how long the string literal is. If you don't already know how std::string and the compiler work, there is no way to know that the compiler can't make use of its knowledge about the size of the literal. – John Knoeller Jan 31 '10 at 23:07 I know that it cant be optimised automatically using c++ syntax, thats why im asking why the compiler cant, since the compiler knows its a constant string, knows the length of constant strings (or can at least find out at compile time), and could be told about various cases in the c++ standard library where it could substitute one thing for another.

– Fire Lancer Jan 31 '10 at 23:19 3 Well, the compiler could. But it would take a lot of effort on the part of the compiler writers and would bind the compiler tightly to the library - I suppose the compiler writers didn't think it was worth it, correctly IMHO . – anon Jan 31 '10 at 23:22 1 The compiler certainly could, it would just have to extend the standard to do so.

String constants can't be templates, so you can't just make a templated constructor. Default arguments can't rely on other arguments so you can't sneak a call to strlen() in. In C++0x you could perhaps have a literal class containing the string length, but there would be no way to construct it from a normal string literal.

This is genuinely a failure at the language level, although not a serious one. – Potatoswatter Jan 31 '107 at 3:12.

The compiler undoubtedly could do something like this, and actually you could do this yourself: template std::string f(const char(&c)SIZE) { return std::string(c, SIZE); } int main() { std::string s = f(" } or even with a custom derived type (though there is no reason std::string couldn't have this constructor): class mystring : public string { public: template mystring(const char(&c)SIZE) : string(c, SIZE) {} }; int main() { mystring s(" } One large drawback is that a version of the function/constructor is generated for every different string size, and the whole class could even be duplicated if the compiler doesn't handle template hoisting very well... These could be deal-breakers in some situations.

SIZE should be size_t, not int. We can't have a string of -1 length. – Chris Lutz Feb 1 '10 at 3:33 If you insist!

I didn't realize I had -pedantic turned on :) – joshperry Feb 1 '10 at 3:39 @joshperry - I always have -pendantic turned on. :P – Chris Lutz Feb 1 '10 at 3:40 D'oh! I forgot that string literals are arrays.

§2.13.4/1: "string literal has type “array of n const char”" Erasing my answer… – Potatoswatter Feb 1 '10 at 4:06 Looks unlikely that the compiler will generate additional code for this. Remember that functions declared inside class{} are inline, and you're not declaring a new specialization of string so string methods will not be duplicated. This should be zero-overhead on most platforms.

– Potatoswatter Feb 1 '10 at 4:10.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions