CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2014
    Posts
    37

    [RESOLVED] unfamiliar function declaration

    I am experimenting with the c++11 regex and came across an item missing from my limited c++ knowledge.

    From this article:
    http://msdn.microsoft.com/en-us/magazine/dn519920.aspx

    I am unfamiliar with this function with the -> operator?
    Code:
    auto begin(strip const & s) -> char const *
    {
      return s.first;
    }
    Is this code the same?

    Code:
    char const* begin (strip const&  s)
    {
        return s.first;
    }

    James

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: unfamiliar function declaration

    Yes. This form of specifying the return type now tends to be used with templates.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jul 2002
    Posts
    2,543

    Re: unfamiliar function declaration

    Yes, these two declarations are the same, and in this context there is no need to use the first one (though the code is correct). auto return type with -> return-type syntax is used in templates. You can read explanation here: http://stackoverflow.com/questions/2...nction-heading
    This is short summary. Consider template function with T1 and T2 parameters, which returns the value of type t1 + t2 (t1 is variable of T1 type, and t2 is variable of T2 type). t1+t2 type is unknown before template instantiation. There are two ways to write such template:
    Code:
    template <typename T1, typename T2>
    decltype(std::declval<T1>() + std::declval<T2>())
    Add(T1 a, T2 b) 
    { 
         return a + b; 
    }

    Code:
    template <typename T1, typename T2>
    auto Add(T1 a, T2 b) -> decltype(a + b)
    {
         return a + b; 
    }
    The second version is more readable.
    Last edited by Alex F; December 31st, 2014 at 09:28 AM.

  4. #4
    Join Date
    Nov 2014
    Posts
    37

    Re: unfamiliar function declaration

    Thank you gentlemen.
    My education continues.

    James

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