Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<functional>: Clang error when calling target() on a const function #3843

Closed
StephanTLavavej opened this issue Jun 30, 2023 · 4 comments · Fixed by #3844
Closed

<functional>: Clang error when calling target() on a const function #3843

StephanTLavavej opened this issue Jun 30, 2023 · 4 comments · Fixed by #3844
Labels
bug Something isn't working fixed Something works now, yay!

Comments

@StephanTLavavej
Copy link
Member

Repros with VS 2022 17.7 Preview 2 x64 containing Clang 16.0.1.

D:\GitHub\STL\out\x64>type meow.cpp
#include <cassert>
#include <functional>
using namespace std;

int main() {
    const function<void()> f;

    assert(f.target<void()>() == nullptr);
}
D:\GitHub\STL\out\x64>cl /EHsc /nologo /W4 /MTd /Od meow.cpp && meow
meow.cpp

D:\GitHub\STL\out\x64>clang-cl /EHsc /nologo /W4 /MTd /Od meow.cpp && meow
In file included from meow.cpp:2:
D:\GitHub\STL\out\x64\out\inc\functional(1142,16): error: reinterpret_cast from 'const void *' to 'void (*)()' casts
      away qualifiers
        return reinterpret_cast<const _Fx*>(this->_Target(typeid(_Fx)));
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
meow.cpp(8,14): note: in instantiation of function template specialization 'std::function<void ()>::target<void ()>'
      requested here
    assert(f.target<void()>() == nullptr);
             ^
1 error generated.

Reduced from the original report DevCom-10402530 VSO-1843867 AB#1843867 "std::function::target is broken on clang-cl when called on const objects".

@StephanTLavavej StephanTLavavej added the bug Something isn't working label Jun 30, 2023
@cpplearner
Copy link
Contributor

cpplearner commented Jun 30, 2023

IIUC f.target<void()>() could unconditionally return nullptr. The target has a decayed type and thus can't be a function ([func.wrap.func.con]/13).

@AlexGuteniev
Copy link
Contributor

IIUC f.target<void()>() could unconditionally return nullptr. The target has a decayed type and thus can't be a function ([func.wrap.func.con]/13).

I understand that the standard states this.
But I'm not sure that there was an intention to say this.

@cpplearner
Copy link
Contributor

I don't think the standard cares about impossible types. It also allows f.target<int>(), for example.

@frederick-vs-ja
Copy link
Contributor

But I'm not sure that there was an intention to say this.

LWG-3305 seems to be related, but the resolution seemly states that MSVC STL is just fine, and we should continue to accept function types and return null function pointers for them.

@StephanTLavavej StephanTLavavej added the fixed Something works now, yay! label Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants