jeudi 12 décembre 2019

is_trivially_destructible: invalid use of incomplete type ‘class Bar2’

For example I need some a wrapper class, one of it's jobs is to tell me if the container is_trivially_destructible or not:

template<typename T, typename = void>
class Foo {
 public:
  Foo(T *t) {
    std::cout << "a trivial" << std::endl;
  }
};

template<typename T>
class Foo<T, typename std::enable_if_t<!std::is_trivially_destructible<T>::value>> {
 public:
  Foo(T *t) {
    std::cout << "not a trivial" << std::endl;
  }
};

and two test class:

class Bar1 {
};

class Bar2 {
 public:
  ~Bar2() {}
};

and it works fine:

int main() {
  Bar1 bar1;
  Bar2 bar2;
  Foo<Bar1> foo1(&bar1);
  Foo<Bar2> foo2(&bar2);
}

but if the test class got more complicated:

class Bar2 {
 public:
  Bar2() : foo(nullptr) {}
  Foo<Bar2> foo;
  ~Bar2() {}
};

I got an error:

error: invalid use of incomplete type ‘class Bar2’

I guess that until the end of declaration of class Bar2, the Bar2 class is incomplete, so access Bar2 in declaration of Bar2 is forbiden.

So is this a wrong design pattern? If not, how can I fix this problem?

Aucun commentaire:

Enregistrer un commentaire