If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

This is part of a larger program so in reality StringHelper has more static functions but this example does produce the error the same as the code when in the larger program.

To get it to compile under g++ I had to assign the return value from substr() to a string and pass that string to stringToNumeric. Why do I have to do this for g++ and not VS? Do I have something wrong with my template function that g++ is calling out and VS is not?

Re: Compile Error with Static Template Function (VS vs. g++)

g++ is right, the reason being that you cannot bind an lvalue reference to an rvalue ( in other words, you cannot pass the temporary returned by substr() to an argument of type std::string& ). Unfortunately, VC++ has a language extension enabling that for non scalar types ( AFAIK, it was used to support a sort of move-semantics emulation in pre-C++11 ) ...

so the solution is to either change stringToNumeric to accept an "std::string const&" or to assign the substr() result to a named string variable:

Re: Compile Error with Static Template Function (VS vs. g++)

Originally Posted by Yadrif

The below code compiles without error using VS 2012

I am quite sure that with Warning level set to highest, that you get the "non-standard extension" warning with Visual Studio 2012. I'm assuming this, since Visual Studio 2008 certainly would warn you of this binding error (but only if the warning level is set to 4).

Re: Compile Error with Static Template Function (VS vs. g++)

I did not know about the warning level. I made the change.

Also, I read about lvalue and rvalue and now understand more what is going on. I would like to mark the argument as const but in the real code I am changing the passed in string so I was not able to. I need to remove the other stuff from the stringToNumeric function and do it prior to calling.

Re: Compile Error with Static Template Function (VS vs. g++)

Originally Posted by Yadrif

[...]but in the real code I am changing the passed in string

well, if the name and purpose of the actual function make totally clear that the argument is going to be copied anyway then you could just pass by value; in this way, the code will be even more correct and more performing thanks to copy elision ( or explicit move in c++11 ).