public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104358] New: Assignable template lambda as function parameter is incorrectly reduced to type of "int"
@ 2022-02-03  1:24 nickhuang99 at hotmail dot com
  2022-02-03 20:14 ` [Bug c++/104358] " nickhuang99 at hotmail dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: nickhuang99 at hotmail dot com @ 2022-02-03  1:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104358

            Bug ID: 104358
           Summary: Assignable template lambda as function parameter is
                    incorrectly reduced to type of "int"
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nickhuang99 at hotmail dot com
  Target Milestone: ---

Please see code at https://www.godbolt.org/z/o3bxxsYPK
All other compilers parses this correctly.


#include <functional>
#include <type_traits>

using namespace std;

//1. I have a callback which happens to be an assignable lambda
template<typename T>
using Lambda=decltype(+[](T){});
//  which is esstially a function pointer 
static_assert(is_same_v<Lambda<int>, void(*)(int)>);

//2. Here is my data handle function which takes a data and the callback 
template<typename T>
auto foo(T&&, Lambda<T>)->T;

//3. However, parser consider the callback as type of "int"
static_assert( ! is_same_v<decltype(&foo<int>), int(*)(int&&, void(*)(int))>);
// lambda type is always considered as "int"
static_assert(is_same_v<decltype(&foo<int>), int(*)(int&&, int)>);
static_assert(is_same_v<decltype(&foo<bool>), bool(*)(bool&&, int)>);

// this is impossible as callback CANNOT be "int"
static_assert( ! is_same_v<Lambda<int>, int>);


////////////////////////////////////////////
//4. This won't happen for a traditional function pointer
template<typename T>
using FunPtr=void(*)(T);
static_assert(is_same_v<FunPtr<int>, void(*)(int)>);

//5. let's define a similar function with callback
template<typename T>
auto bar(T&&, FunPtr<T>)->T;
// there is no way for parser to consider callback as type of "int"
static_assert(is_same_v<decltype(&bar<int>), int(*)(int&&, void(*)(int))>);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/104358] Assignable template lambda as function parameter is incorrectly reduced to type of "int"
  2022-02-03  1:24 [Bug c++/104358] New: Assignable template lambda as function parameter is incorrectly reduced to type of "int" nickhuang99 at hotmail dot com
@ 2022-02-03 20:14 ` nickhuang99 at hotmail dot com
  2022-02-05 22:46 ` nickhuang99 at hotmail dot com
  2022-10-27  5:21 ` [Bug c++/104358] signature of template function with template lambda(converted to function pointer) mistakenly regards it as " pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: nickhuang99 at hotmail dot com @ 2022-02-03 20:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104358

--- Comment #1 from qingzhe huang <nickhuang99 at hotmail dot com> ---
Sorry about the long description and here is the short version to highlight the
core issue. Given this template function with a templated lambda as parameter:

template<typename T>
using Lambda=decltype(+[](T){});

template<typename T>
auto foo(T&&, Lambda<T>)->T;



GCC considers the parameter "Lambda<T>" as type "int" which is wrong:

static_assert(is_same_v<decltype(&foo<int>), 
                 int(*)(int&&, /*"Lambda<T>"*/ int)>);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/104358] Assignable template lambda as function parameter is incorrectly reduced to type of "int"
  2022-02-03  1:24 [Bug c++/104358] New: Assignable template lambda as function parameter is incorrectly reduced to type of "int" nickhuang99 at hotmail dot com
  2022-02-03 20:14 ` [Bug c++/104358] " nickhuang99 at hotmail dot com
@ 2022-02-05 22:46 ` nickhuang99 at hotmail dot com
  2022-10-27  5:21 ` [Bug c++/104358] signature of template function with template lambda(converted to function pointer) mistakenly regards it as " pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: nickhuang99 at hotmail dot com @ 2022-02-05 22:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104358

--- Comment #2 from qingzhe huang <nickhuang99 at hotmail dot com> ---
Here are more tests (https://www.godbolt.org/z/5qc8jTGa8) to show that only
msvc is giving the correct result. 

It just illustrates the use cases of this definition and why it should be
allowed.

1. By using operator "+", here the type "Lambda" is just a free function
pointer.

template<typename T>
using Lambda=decltype(+[](T){});
static_assert(is_same_v<Lambda<int>, void(*)(int)>); 

2. The "foo" takes "Lambda" as parameter
template<typename T>
T foo(T&&, Lambda<T>); 

3. As "Lambda<T>" is just a function pointer, "foo" should allow both free
function pointer or lambda(by conversion operator) to be used as argument. And
because GCC mistakenly consider "foo<int>" signature as "int(*)(int&&,
/*Lambda<int>*/int)", this is not possible.

static_assert(is_same_v<decltype(&foo<int>), int(*)(int&&, int)>); 

These are use cases for "foo":
a) using lambda directly
    foo<int>(5, [](int n){});// lambda conversion operator!
b) using converted function pointer from lambda by operator "+"
    auto funptr=+[](int n){};
    foo<int>(5, funptr);
c) directly using a function pointer
    void(*ptr)(int)=funptr;
    foo<int>(5, ptr);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/104358] signature of template function with template lambda(converted to function pointer) mistakenly regards it as type of "int"
  2022-02-03  1:24 [Bug c++/104358] New: Assignable template lambda as function parameter is incorrectly reduced to type of "int" nickhuang99 at hotmail dot com
  2022-02-03 20:14 ` [Bug c++/104358] " nickhuang99 at hotmail dot com
  2022-02-05 22:46 ` nickhuang99 at hotmail dot com
@ 2022-10-27  5:21 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-27  5:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104358

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |107430
           Keywords|                            |c++-lambda, rejects-valid,
                   |                            |wrong-code

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There is a definitely a few dups of this bug.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107430
[Bug 107430] [meta-bug] lambda in decltype

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-10-27  5:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-03  1:24 [Bug c++/104358] New: Assignable template lambda as function parameter is incorrectly reduced to type of "int" nickhuang99 at hotmail dot com
2022-02-03 20:14 ` [Bug c++/104358] " nickhuang99 at hotmail dot com
2022-02-05 22:46 ` nickhuang99 at hotmail dot com
2022-10-27  5:21 ` [Bug c++/104358] signature of template function with template lambda(converted to function pointer) mistakenly regards it as " pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).