https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108565

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |EH

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is also exceptions related, that is -fno-exceptions causes no warning to
show up and also the function optimized too.

Also using malloc/free works too:
```
#if 1
int *mymalloc(int t)
{
    int *a = (int*)__builtin_malloc(sizeof(int));
    *a = t;
    return a;
}
void myfree(int *a)
{
    __builtin_free(a);
}
#else
#include <new>
int *mymalloc(int t)
{
    return new int(t);
}
void myfree(int *a)
{
    delete a;
}
#endif
struct shared_ptr {
    int *counter_ = nullptr;
    int *data_ = nullptr;
    shared_ptr(int *data)
    {
       // try {
        counter_ = (data ? mymalloc(1) : nullptr);
       // }catch(...) {
      //      counter_ = nullptr;
           // throw;
       // }
        data_ = (data);
    }
    shared_ptr(const shared_ptr &other)
        : counter_(other.counter_), data_(other.data_) {
        if (counter_ != nullptr) {
            ++*counter_;
        }
    }
    ~shared_ptr() {
        if (counter_ != nullptr) {
            --*counter_;
            if (*counter_ == 0) {
                myfree(counter_);
                myfree(data_);
            }
        }
    }
};
void foo() {
    shared_ptr a(mymalloc(10));  // should be non-nullptr
    shared_ptr b(a);
    shared_ptr c(mymalloc(20));  // should be non-nullptr
}
int main() {
    foo();
}
```

Reply via email to