CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2022
    Posts
    1

    decltype accepting two parameters?

    I got fairly good grasp of decltype, inferencing type. But most of the examples online, the functions accepting one parameter. I came across code which accepts two parameters and struggling. I modified the code a bit to make it confidential:

    Code:
    template <class T>
    auto <function_name>(T&& x, status_t err = statusBadParm)
        -> decltype((x == nullptr), get_object(*x))
    {
        if(x == nullptr)
        {
          // add'l code
        }
        return get_object(*x);
    }
    Last edited by VictorN; April 17th, 2022 at 03:28 AM. Reason: added CODE tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,265

    Re: decltype accepting two parameters?

    I added CODE tags around your code snippet to make the code readable.

    Now we're waiting for your explanation about what you need, what you could not achieve, and so on...
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,646

    Re: decltype accepting two parameters?

    decltype() does only accept one param. In your example

    Code:
    decltype((x == nullptr), get_object(*x))
    decltype() has only one param get_object(*x). , is the comma operator so that the expressions separated by a comma are evaluated left to right with the final final being the value of the rightmost expression. As (x == nullptr) has no side-effects, the result is discarded. So this is the same as:

    Code:
    decltype(get_object(*x))
    For decltype(), you don't need to check that x is not nullptr as the value is not evaluated there. Only the return type of get_object() is obtained by decltype().
    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.2.4)

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