CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Oct 2007
    Posts
    34

    [RESOLVED] Compile Error with Static Template Function (VS vs. g++)

    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.
    Last edited by Yadrif; November 29th, 2012 at 09:48 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured