SORU
31 Mart 2012, CUMARTESİ


tamamlanmamış bir tür't kazandı derleme::unique_ptr std

std::unique_ptr ile pimpl-deyim kullanıyorum:

class window {
  window(const rectangle& rect);

private:
  class window_impl; // defined elsewhere
  std::unique_ptr<window_impl> impl_; // won't compile
};

Ancak, ben bir satırda hata tamamlanmamış bir tür kullanımı ile ilgili, derleme <memory> 304:

'sizeof' tamamlanmamış bir tür 'uixx::window::window_impl' . geçersiz uygulama

Bildiğim kadarıyla std::unique_ptr eksik bir türü ile kullanılması gerekir. Bu kütüphanenin bir hata ya da yanlış bir şey mi yapıyorum?

CEVAP
31 Mart 2012, CUMARTESİ


Burada eksik türleri ile std::unique_ptr bazı örnekler. Sorun yok yatıyor.

Eğer unique_ptr ile pimpl kullanırsanız bir yıkıcı bildirmek gerekir:

class foo
{ 
    class impl;
    std::unique_ptr<impl> impl_;

public:
    foo(); // You may need a def. constructor to be defined elsewhere

    ~foo(); // Implement (with an empty body) where impl is complete
};

çünkü aksi takdirde derleyici varsayılan bir oluşturur, ve bunun için foo::impl tam bir beyan ihtiyacı var.

Eğer şablon kurucular varsa, o zaman impl_ üye oluşturmanız don bile hata.

template <typename T>
foo::foo(T bar) 
{
    // Here the compiler needs to know how to
    // destroy impl_ in case an exception is
    // thrown !
}

Ad kapsamı, unique_ptr kullanarak ya da işe yaramaz:

class impl;
std::unique_ptr<impl> impl_;

bu süre statik bir nesne nasıl yok derleyici beri burada bilmeniz gerekir. Geçici bir çözüm değildir:

class impl;
struct ptr_impl : std::unique_ptr<impl>
{
    ~ptr_impl(); // Implement (empty body) elsewhere
} impl_;

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • ICON

    ICON

    19 EKİM 2011
  • jat4011

    jat4011

    16 EKİM 2010
  • TechXCentral

    TechXCentral

    12 Temmuz 2011