Stl: <functional>: std::is_convertible is broken for std::bind functors

Created on 1 Jul 2020  路  6Comments  路  Source: microsoft/STL

Describe the bug
std::is_convertible fails to compile broken for a lambda passed to std::bind

Command-line test case

D:\Temp2>type repro.cpp
#include <functional>
#include <iostream>

int main() {
    auto l = [](int) {};
    auto f = std::bind(l, std::placeholders::_1);
    std::cout << std::is_convertible<decltype(f), std::function<void()>>::value << std::endl;
}

D:\Temp2>cl /EHsc /W4 /WX .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29009.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

repro.cpp
c:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29009\include\utility(487): error C2338: tuple index out of bounds
c:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29009\include\functional(1371): note: see reference to class template instantiation 'std::tuple_element<0,std::tuple<>>' being compiled
c:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29009\include\tuple(672): note: see reference to alias template instantiation 'std::tuple_element_t<0,std::tuple<>>' being compiled
c:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29009\include\tuple(790): note: see reference to function template instantiation 'const tuple_element<_Index,std::tuple<_Rest...>>::type &&std::get(const std::tuple<_Rest...> &&) noexcept' being compiled
c:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29009\include\type_traits(1720): note: see reference to alias template instantiation 'std::_Decltype_invoke_zero<std::_Binder<std::_Unforced,main::<lambda_47bb48da0a3b46550689dee1f28f59ab> &,const std::_Ph<1> &>&>' being compiled
c:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29009\include\type_traits(1796): note: see reference to alias template instantiation 'std::_Is_invocable_r_<void,std::_Binder<std::_Unforced,main::<lambda_47bb48da0a3b46550689dee1f28f59ab> &,const std::_Ph<1> &>&,>' being compiled
c:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29009\include\functional(961): note: see reference to class template instantiation 'std::_Is_invocable_r<_Ret,_From &>' being compiled
        with
        [
            _Ret=void,
            _From=std::_Binder<std::_Unforced,main::<lambda_47bb48da0a3b46550689dee1f28f59ab> &,const std::_Ph<1> &>
        ]
c:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29009\include\functional(961): note: see reference to variable template 'const bool conjunction_v<std::negation<std::is_same<std::_Binder<std::_Unforced,<lambda_47bb48da0a3b46550689dee1f28f59ab> &,std::_Ph<1> const &>,std::function<void __cdecl(void)> > >,std::_Is_invocable_r<void,std::_Binder<std::_Unforced,<lambda_47bb48da0a3b46550689dee1f28f59ab> &,std::_Ph<1> const &> &> >' being compiled
c:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29009\include\utility(487): note: see reference to alias template instantiation 'std::_Func_class<_Ret>::_Enable_if_callable_t<_From&,std::function<void (void)>>' being compiled
        with
        [
            _Ret=void,
            _From=std::_Binder<std::_Unforced,main::<lambda_47bb48da0a3b46550689dee1f28f59ab> &,const std::_Ph<1> &>
        ]
.\repro.cpp(7): note: see reference to class template instantiation 'std::is_convertible<std::_Binder<std::_Unforced,main::<lambda_47bb48da0a3b46550689dee1f28f59ab> &,const std::_Ph<1> &>,std::function<void (void)>>' being compiled

Expected behavior
The program should compile.
(It should print zero, as the types are not expected to be convertible)

STL version

Microsoft Visual Studio Professional 2019 Preview
Version 16.7.0 Preview 3.1

Additional context
This item is also tracked on Developer Community as DevCom-317991 and by Microsoft-internal VSO-671111 / AB#671111.

bug

Most helpful comment

After WG21-P1065, bind's specification now uses _expression-equivalent_, which I think requires SFINAE?

All 6 comments

I'm tagging this "enhancement" rather than "bug" because the expected behavior is not Standard-mandated behavior so much as a desirable extension.

I don't understand why it is not Standard-mandated.

It's because nothing in bind()'s specification requires its function call operator to SFINAE away for un-callable arguments. (This is because bind() is ancient and predates result_of SFINAE.)

I'm 95% sure that this analysis is still correct. If the Standardese actually requires bind() to SFINAE, please let me know.

After WG21-P1065, bind's specification now uses _expression-equivalent_, which I think requires SFINAE?

After WG21-P1065, bind's specification now uses _expression-equivalent_, which I think requires SFINAE?

Close enough to it, yes. So let's call this a bug.

Oh yeah, I noticed expression-equivalent's conditional noexcept implication while reviewing #703 (which @AdamBucior then implemented), I should have realized it implies SFINAE too. Thanks!

Was this page helpful?
0 / 5 - 0 ratings