The below code compiles without error using VS 2012 but with g++ 4.1.2 I get this error:
Code:
main.cpp: In function 'int main(int, char**)':
main.cpp:37: error: no matching function for call to 'StringHelper::stringToNumeric(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Here is the code:
Code:
#include <string>
#include "boost/lexical_cast.hpp"
using boost::lexical_cast;
using boost::bad_lexical_cast;
class StringHelper
{
public:
// Modify in place and return to enable assignment
template<typename T>
static T stringToNumeric(std::string& value)
{
T retval;
try
{
retval = boost::lexical_cast<T>(value);
}
catch(bad_lexical_cast& ex)
{
retval = boost::lexical_cast<T>(0);
}
return retval;
}
};
int main(int argc, char* argv[])
{
std::string s("1234");
int i = StringHelper::stringToNumeric<int>(s.substr(0,2));
return 0;
}
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?
Any help is appreciated.
Thanks.