다음과 같이 호출 가능한 유형이 있다고 가정하십시오.
struct mutable_callable
{
int my_mutable = 0;
int operator()() { // Not const
return my_mutable++;
}
};
참고 mutable_callable비 const가이 operator()수정 멤버 변수 것을 .....
이제 std::function내 유형을 벗어난 것으로 가정하십시오 .
std::function<int()> foo = mutable_callable{};
이제 나는 이것을 할 수 있습니다 :
void invoke(std::function<int()> const& z)
{
z();
}
int main()
{
invoke(foo); // foo changed.....oops
}
지금까지 내가 말할 수있는 std::function들 operator()입니다 const: 당
) (https://en.cppreference.com/w/cpp/utility/functional/function/operator
내 직감은 당신이 이것을 할 수 없어야한다는 것입니다 .....
그러나 다음을보고 : https://en.cppreference.com/w/cpp/utility/functional/function/function
이것은 호출 가능한 유형에 상수가 있는지 여부에 대한 제약을 두지 않는 것 같습니다 operator()...
그래서 내 질문은 이것입니다 : 나는 std::function<int()> const&본질적으로 std::function<int()>&두 사람의 행동 사이에 실제로 차이가 없다는 것과 똑같은 것으로 가정하고 정확 합니다 ... 그리고 그 경우에 왜 const정확 하지 않습니까?
std::function구현 내부에 대한 작은 스 니펫입니다 . i.stack.imgur.com/eNenN.png 여기서 using _Ptrt = _Func_base<_Ret, _Types...>. 난 내 경우를 휴식.
std::functionA와 동등한있다struct a{ std::any x; };..... 거기에를