pimpl 习语和模板类朋友

发布于 2024-08-08 05:09:20 字数 2020 浏览 8 评论 0原文

我试图使用 pimpl 习惯用法来隐藏一些蹩脚的模板代码,但我无法让主体类友元的派生类访问句柄类。我从 MSVC 9 sp1 收到错误 C2248。下面是一些复制错误的代码:

//
// interface.hpp
//
namespace internal{
    template<class T>
    class specific_body;
}

class interface
{
    struct body;
    body *pbody_;
    interface(body *pbody);

    template<class T>
    friend class internal::specific_body;

public:

    ~interface();

    interface(const interface &rhs);

    bool test() const;

    static interface create( bool value );
};

//
// interface.cpp
//
struct interface::body
{
    virtual ~body(){}

    virtual bool test() const = 0;

    virtual interface::body *clone() const = 0;
};

class true_struct {};
class false_struct {};

namespace internal {

template< class T>
class specific_body : public interface::body
{ // C2248
public:

    specific_body(){}

    virtual bool test() const;

    virtual interface::body *clone() const
    {
        return new specific_body();
    }
};

bool specific_body<true_struct>::test() const
{
    return true;
}

bool specific_body<false_struct>::test() const
{
    return false;
}

} //namespace internal

interface::interface(body *pbody) : pbody_(pbody) {}

interface::interface(const interface &rhs) : pbody_(rhs.pbody_->clone()) {}

interface::~interface() { delete pbody_; }

bool interface::test() const
{
    return pbody_->test();
}

interface interface::create(bool value )
{
    if ( value )
    {
        return interface(new internal::specific_body<true_struct>());
    }
    else
    {
        return interface(new internal::specific_body<false_struct>());
    }
}

//
// main.cpp
//
// #include "interface.hpp"
//

int _tmain(int argc, _TCHAR* argv[])
{
    interface object( interface::create(true));

    if ( object.test() )
    {
        // blah
    }
    else
    {
    }
    return 0;
}

任何帮助将不胜感激,我正在尝试向 interface< 用户隐藏 interface::bodyspecific_body 实现/code> 如果从我的问题来看这并不明显。

I'm trying to use the pimpl idiom to hide some grungy template code, but I can't give derived classes of the body class friend access to the handle class. I get an error C2248 from MSVC 9 sp1. Here's some code to duplicate the error:

//
// interface.hpp
//
namespace internal{
    template<class T>
    class specific_body;
}

class interface
{
    struct body;
    body *pbody_;
    interface(body *pbody);

    template<class T>
    friend class internal::specific_body;

public:

    ~interface();

    interface(const interface &rhs);

    bool test() const;

    static interface create( bool value );
};

//
// interface.cpp
//
struct interface::body
{
    virtual ~body(){}

    virtual bool test() const = 0;

    virtual interface::body *clone() const = 0;
};

class true_struct {};
class false_struct {};

namespace internal {

template< class T>
class specific_body : public interface::body
{ // C2248
public:

    specific_body(){}

    virtual bool test() const;

    virtual interface::body *clone() const
    {
        return new specific_body();
    }
};

bool specific_body<true_struct>::test() const
{
    return true;
}

bool specific_body<false_struct>::test() const
{
    return false;
}

} //namespace internal

interface::interface(body *pbody) : pbody_(pbody) {}

interface::interface(const interface &rhs) : pbody_(rhs.pbody_->clone()) {}

interface::~interface() { delete pbody_; }

bool interface::test() const
{
    return pbody_->test();
}

interface interface::create(bool value )
{
    if ( value )
    {
        return interface(new internal::specific_body<true_struct>());
    }
    else
    {
        return interface(new internal::specific_body<false_struct>());
    }
}

//
// main.cpp
//
// #include "interface.hpp"
//

int _tmain(int argc, _TCHAR* argv[])
{
    interface object( interface::create(true));

    if ( object.test() )
    {
        // blah
    }
    else
    {
    }
    return 0;
}

Any help would be appreciated, I'm trying to hide interface::body and specific_body implementations from the users of interface if that's not obvious from my question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

软甜啾 2024-08-15 05:09:20

您需要添加模板<>在模板测试方法的显式实例化中

template<> // add this line
bool specific_body<true_struct>::test() const
{
    return true;
}

You need to add template<> in the explicit instantiation of the template test method

template<> // add this line
bool specific_body<true_struct>::test() const
{
    return true;
}
琴流音 2024-08-15 05:09:20

您尚未获得 specific_body 资格。尝试

template<class T>
friend class internal::specific_body;

作为你的朋友宣言。

You haven't qualified specific_body. Try

template<class T>
friend class internal::specific_body;

as your friend declaration.

や三分注定 2024-08-15 05:09:20

尝试使用 typename 也许?我想我在 Sutter 中读到 typename 将能够进入未知范围内的类,而 class 则不会。

Try using typename maybe? I think I read in Sutter that typename will work to get to class inside of an unknown scope, while class won't.

影子是时光的心 2024-08-15 05:09:20

除了 Troubadour 提到的不合格的 Specific_body 之外,您对 true_struct 和 false_struct 的 Specific_body<>::test 的专业化尝试似乎不正确。你必须专注于全班。

为了解决这个问题,我只需在公共部分声明body。另外,将 Specific_body 声明为 interface::body 的友元也没有帮助。

In addition to the unqualified specific_body mentioned by Troubadour, your specialization attempt of specific_body<>::test for true_struct and false_struct seems incorrect. You have to specialice the full class.

To solve the problem, I'd simply declare body in the public section. Declaring specific_body to be a friend of interface::body in addition doesn't help either.

围归者 2024-08-15 05:09:20

好吧,我能够通过将 body 设为接口中的公共声明来“解决”这个问题。这解决了声明 specific_body 期间的 C2248 错误。我还使 body 成为 interface 类的友元,并向 body 结构添加了一个方法:

static interface create( body *pbody )
{
    return interface(pbody);
}

以便生成 specific_body如果 specific_body 实例之间存在嵌套关系,code> 可以创建一个接口

Well, I was able to "solve" this problem by making the body a public declaration in the interface. That solves the C2248 error during the declaration of the specific_body. I also made the body a friend to the interface class and added a method to the body struct:

static interface create( body *pbody )
{
    return interface(pbody);
}

so that a specific_body can create an interface if there is a nested relationship between instances of specific_body

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文