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))>);

Reply via email to