```
class foo {
    foo();
    unique_ptr<T, (void *)(T*)> ptr;
};

foo::foo() {
    bool x = some operation;
    if (!x) throw;
    ptr = unique_ptr<T, (void *)(T*)>(new T(x), deleter);
}

```

This code does not compile because the unique pointer and its deleter get initialized with null (as I don't initialize it in the initializer list). I can't initialize it in the list because it's dependent on the variable `x` as shown above. How can I solve this deadlock?
['https://Stackoverflow.com/questions/35748064', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4558644/']
One solution would be to add a static function that determines what `x` should be and then return it to the constructor of the `unique_ptr` in the member initialization list

```
foo() : ptr(construct_t(), deleter) {}
static T* construct_t()
{
    bool x = some operation;
    if(!x) throw;
    return new T(x);
}

```
working example of using a generator and custom deleter:

```
#include <iostream>
#include <memory>
#include <vector>

struct bar {};

struct bar_deleter {
    void operator()(bar* p) const noexcept {

        try {
            if (p) {
                std::cout << "deleting p" << std::endl;
            }
            else {
                std::cout << "not deleting p" << std::endl;
            }
        }
        catch(...) {
        }
    }
};

bool some_condition()
{
    static int counter = 0;
    return (counter++ % 2) == 0;
}

struct foo {
    using ptr_type = std::unique_ptr<bar, bar_deleter>;

    foo()
    : ptr(bar_generator())
    {}

    static ptr_type bar_generator() {
        if (some_condition()) {
            std::cout << "creating bar" << std::endl;
            return { new bar, bar_deleter{} };
        }
        else {
            std::cout << "not creating bar" << std::endl;
            return { nullptr, bar_deleter{} };
        }
    }

    ptr_type ptr;
};

int main()
{
    using namespace std;
    auto v = std::vector<foo>(10);
    return 0;
}

```

expected results:

```
creating bar
not creating bar
creating bar
not creating bar
creating bar
not creating bar
creating bar
not creating bar
creating bar
not creating bar
deleting p
deleting p
deleting p
deleting p
deleting p

```