I was looking at this answer and wanted to use. However, I get a segmentation fault, when using the static_cast and const_cast, but if I use a temp variable everything is fine. It is obviously because the non-const version of bar() calls it self over-and-over. But I though the static_cast would result in a const foo* and then choose the const version of bar(). Why is that so?
#include <iostream>
using namespace std;
class foo
{
    public:
    void bar() const
    {
        cout << "const" << endl;
    }
    void bar()
    {
        cout << "non-const" << endl;
//      static_cast<const decltype(this)>(this)->bar();
//      const_cast<const decltype(this)>(this)->bar();
        const auto& tmp = *this;
        tmp.bar();
    }
};
int main() {
    foo A;
    A.bar();
    const foo B;
    B.bar();
    static_cast<const foo*>(&A)->bar();
    return 0;
}
 
     
    