如何使升压:: make_shared我班的一个朋友 [英] How to make boost::make_shared a friend of my class

查看:148
本文介绍了如何使升压:: make_shared我班的一个朋友的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写有受保护的构造一类,因此新的实例只能与返回的shared_ptr的到我的类的静态创建()函数来生产。为了提供有效的分配,我想使用boost ::在创建函数内部make_shared,但是编译器会抱怨,我的类构造函数里面保护的boost :: make_shared。我决定把我的boost :: make_shared我班的一个朋友,但我疑惑的语法。我试过

I have written a class with protected constructor, so that new instances can only be produced with a static create() function which returns shared_ptr's to my class. To provide efficient allocation I'd like to use boost::make_shared inside the create function, however the compiler complains that my class constructor is protected inside boost::make_shared. I decided to my boost::make_shared a friend of my class but I'm puzzled about the syntax. I tried

template< class T, class A1, class A2 >
friend boost::shared_ptr<Connection> boost::make_shared(const ConnectionManagerPtr&, const std::string&);

但是编译器红粉我的语法错误。请帮助。

but the compiler gived me syntax errors. Please help.

推荐答案

您不需要模板的朋友部分,但是你需要以表示朋友函数是一个模板:

You don't need to template the friend part, but you need to signify that the friend function is a template:

friend boost::shared_ptr<Connection> boost::make_shared<>(/* ... */);
//                                                     ^^

这与科莫和当前版本的GCC工作,但失败,VC。更好的将是如下形式:

That works with Comeau and current GCC versions but fails with VC. Better would be the following form:

friend boost::shared_ptr<Connection> boost::make_shared<Connection>(/* ... */);

在多个编译器现在工作 - 我测试了VC8,VC10,GCC 4.2,GCC 4.5和4.3科莫

That works across multiple compilers now - i tested it on VC8, VC10, GCC 4.2, GCC 4.5 and Comeau 4.3.

或者使用一个合格的名字来引用函数模板的特定实例为马丁做的的工作,并与科莫的做法,但GCC扼流圈就可以了。

Alternatively using a qualified name to refer to a particular instance of the function template as Martin does should work and does with Comeau, but GCC chokes on it.

这是不依赖于的实施细则make_shared()(因而也适用于<一个有用的替代href=\"http://stackoverflow.com/questions/3541632/using-make-shared-with-a-protected-constructor-abstract-interface/3541733#3541733\">VC10s TR1实现)是使用pass-key-idiom为构造函数的访问保护,并交好创建()函数来代替,例如:

A useful alternative that doesn't depend on the implementation details of make_shared() (and thus also works with VC10s TR1 implementation) is to use the pass-key-idiom for access-protection of the constructor and to befriend the create() function instead, e.g.:

class Connection {
// ...
public:
    class Key {
        friend boost::shared_ptr<Connection> create(const ConnectionManagerPtr&, 
                                                    const std::string&);
        Key() {}
    };
    Connection(const ConnectionManagerPtr&, const std::string&, const Key&);
};

boost::shared_ptr<Connection> create(const ConnectionManagerPtr& p, 
                                     const std::string& s) 
{
    return boost::make_shared<Connection>(p, s, Connection::Key());
}

这篇关于如何使升压:: make_shared我班的一个朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆