The following code can be compiled with g++ 8.1.0 and clang 10.0.0.
#include <memory>
#include <iostream>
#include <functional>
int main() {
auto dereference = std::bind(
&std::shared_ptr<int>::operator*,
std::placeholders::_1);
std::shared_ptr<int> sp = std::make_shared<int>(10);
std::cout << dereference(sp) << std::endl;
return 0;
}
However, msvc in vs2022 reports the following error:
example.cpp<source>(6): error C2672: 'std::bind': no matching overloaded function foundC:/data/msvc/14.34.31931-Pre/include\functional(2029): note: could be 'std::_Binder<_Ret,_Fx,_Types...> std::bind(_Fx &&,_Types &&...)'<source>(8):
note: 'std::_Binder<_Ret,_Fx,_Types...> std::bind(_Fx &&,_Types &&...)': could not deduce template argument for '_Ret'<source>(8): note: 'std::_Binder<_Ret,_Fx,_Types...> std::bind(_Fx &&,_Types &&...)': could not deduce template argument for '_Fx'C:/data/msvc/14.34.31931-Pre/include\functional(2024): note: or 'std::_Binder<std::_Unforced,_Fx,_Types...> std::bind(_Fx &&,_Types &&...)'<source>(8): note: 'std::_Binder<std::_Unforced,_Fx,_Types...> std::bind(_Fx &&,_Types &&...)': could not deduce template argument for '_Fx'<source>(11): error C3536: 'dereference': cannot be used before it is initialized<source>(11): error C2064: term does not evaluate to a function taking 1 argumentsCompiler returned: 2
Is it a bug of msvc compiler or the above code can be fixed?
To reproduce the compile error, you can go to https://godbolt.org/ for details.
auto dereference = std::bind( &std::shared_ptr::operator*<>, std::placeholders::_1);
– jls28 Nov 19 '22 at 08:32