• Re: Why is that a function-declaration

    From Paavo Helde@21:1/5 to Bonita Montero on Sun Jul 14 21:03:36 2024
    On 14.07.2024 14:46, Bonita Montero wrote:
    My compiler considers this as a function declaration:

        thread_pool::stop_token stopper( true_type() );

    I'd understand that without the "()" the compiler considers
    this a  declaration, but not with the "()".

    https://isocpp.org/wiki/faq/ctors#fn-decl-vs-obj-instantiation

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andrey Tarasevich@21:1/5 to Bonita Montero on Mon Jul 15 07:14:32 2024
    On 07/14/24 4:46 AM, Bonita Montero wrote:
    My compiler considers this as a function declaration:

        thread_pool::stop_token stopper( true_type() );

    I'd understand that without the "()" the compiler considers
    this a  declaration, but not with the "()".

    Parameters of function type are legal in C++. `true_type()` declares
    exactly that.

    So, `stopper` is a function that takes a single parameter of type `true_type()`. The latter is a function with no parameters, returning `true_type`.

    --
    Best regards,
    Andrey

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andrey Tarasevich@21:1/5 to Bonita Montero on Mon Jul 15 19:51:04 2024
    On 07/15/24 8:46 AM, Bonita Montero wrote:
    Am 15.07.2024 um 16:14 schrieb Andrey Tarasevich:
    On 07/14/24 4:46 AM, Bonita Montero wrote:
    My compiler considers this as a function declaration:

         thread_pool::stop_token stopper( true_type() );

    I'd understand that without the "()" the compiler considers
    this a  declaration, but not with the "()".

    Parameters of function type are legal in C++. `true_type()` declares
    exactly that.

    So, `stopper` is a function that takes a single parameter of type
    `true_type()`. The latter is a function with no parameters, returning
    `true_type`.

    Not true. true_type() is a default-parameter for a unnamed parameter
    with not explctitly given name.

    Huh??? Not sure what you are trying to say here,... but no.

    The parameter is indeed unnamed. But its type is exactly as I stated
    above: in this context `true_type()` stands for "a function with no
    parameters, returning `true_type`". Parameters of function type are
    legal in C++ (and in C), they get immediately _adjusted_ to the
    corresponding function-pointer type.

    If you give a name to the parameter (say, `x`), your declaration would
    become

    thread_pool::stop_token stopper( true_type x() );

    `x` is a function with no parameters, returning `true_type`. Due to the aforementioned adjustment this is equivalent to

    thread_pool::stop_token stopper( true_type (*x)() );

    This is what you declared. End of story.

    ---

    Here's a revealing example for you

    struct true_type {};
    struct whatever {};

    int main()
    {
    whatever foo(true_type());
    void *p = foo;
    }

    The GCC's error message reads:

    error: invalid conversion from 'whatever (*)(true_type (*)())' to 'void*'

    https://coliru.stacked-crooked.com/a/a1f11719de8cb94b

    See the parameter type?

    --
    Best regards,
    Andrey

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andrey Tarasevich@21:1/5 to Bonita Montero on Tue Jul 16 19:27:13 2024
    On 07/15/24 9:50 PM, Bonita Montero wrote:
    Am 16.07.2024 um 04:51 schrieb Andrey Tarasevich:

    The parameter is indeed unnamed. But its type is exactly as I stated
    above: in this context `true_type()` stands for "a function with no
    parameters, returning `true_type`". ...

    No, it has a default-parameter of true_type().
    There's n separate type declaration or parameter name,

    Sigh... I guess it is time to whip out the word "hopeless" again...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andrey Tarasevich@21:1/5 to Bonita Montero on Wed Jul 17 06:52:09 2024
    On 07/17/24 5:39 AM, Bonita Montero wrote:

    Sigh... I guess it is time to whip out the word "hopeless" again...



    true_type() isn't a type but the instantiation of a temporary
    of a certain type.

    No. `true_type()` in this context is an _ambiguity_, just like your
    whole declaration in question is an ambiguity.

    `true_type()` in this context is an ambiguity between an expression
    (yes, a temporary) and a nameless declaration of a parameter of function
    type.

    And in C++ ambiguities between expressions and declarations are normally resolved in favor of _declarations_. So, in your case `true_type()` is
    treated as a nameless declaration of a function parameter. And your
    entire original declaration is treated as a function declaration (not an
    object declaration).

    --
    Best regards,
    Andrey

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)