CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  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.

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    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:

    Code:
    std::string subs = s.substr(0,2);
    int i = StringHelper::stringToNumeric<int>(subs);

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by Yadrif View Post
    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).

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Oct 2007
    Posts
    34

    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.

    Thanks for the help.

  5. #5
    Join Date
    Oct 2008
    Posts
    1,456

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

    Quote Originally Posted by Yadrif View Post
    [...]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 ).

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