Discussion:
Why is that a function-declaration
(too old to reply)
Bonita Montero
2024-07-14 11:46:52 UTC
Permalink
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 "()".
Paavo Helde
2024-07-14 18:03:36 UTC
Permalink
    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
Andrey Tarasevich
2024-07-15 14:14:32 UTC
Permalink
    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
Bonita Montero
2024-07-15 15:46:24 UTC
Permalink
Post by Andrey Tarasevich
     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.
Andrey Tarasevich
2024-07-16 02:51:04 UTC
Permalink
Post by Bonita Montero
Post by Andrey Tarasevich
     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
Bonita Montero
2024-07-16 04:50:27 UTC
Permalink
Post by 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,
Andrey Tarasevich
2024-07-17 02:27:13 UTC
Permalink
Post by Bonita Montero
Post by 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...
Bonita Montero
2024-07-17 12:39:10 UTC
Permalink
Post by Andrey Tarasevich
Post by Bonita Montero
Post by 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...
true_type() isn't a type but the instantiation of a temporary
of a certain type.
Andrey Tarasevich
2024-07-17 13:52:09 UTC
Permalink
Post by Bonita Montero
Post by Andrey Tarasevich
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
Loading...