首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当试图将std::make_unique声明为我模板类的朋友时,MSVC出错。

当试图将std::make_unique声明为我模板类的朋友时,MSVC出错。
EN

Stack Overflow用户
提问于 2015-11-24 23:25:31
回答 1查看 804关注 0票数 5

显然,今天MSVC正尽力说服我改用clang。但我不会放弃。早些时候,我问这个问题,想知道如何将std::make_unique声明为我们类的friend

在我的简单场景中,我得到了一个很好的答案,实际上,当我在魔杖盒上尝试使用clang时,它编译得很好。

因此,我很高兴回到Visual 2013继续编写代码。我的代码的一部分是:

代码语言:javascript
运行
复制
// other includes
#include <string>
#include <memory>

template <typename Loader, typename Painter, typename MeshT>
class Model
{
public:
    friend std::unique_ptr<Model> std::make_unique<Model>(
        const std::string&,
        const std::shared_ptr<Loader>&,
        const std::shared_ptr<Painter>&);

    // Named constructor
    static std::unique_ptr<Model> CreateModel(
        const std::string& filepath,
        const std::shared_ptr<Loader>& loader,
        const std::shared_ptr<Painter>& painter)
    {
        // In case of error longer than the Lord of the Rings trilogy, use the
        // line below instead of std::make_unique
        //return std::unique_ptr<Model>(new Model(filepath, loader, painter));
        return std::make_unique<Model>(filepath, loader, painter);
    }

// ...
protected:
    // Constructor
    Model(
        const std::string& filepath,
        const std::shared_ptr<Loader>& loader,
        const std::shared_ptr<Painter>& painter)
        : mFilepath(filepath)
        , mLoader(loader)
        , mPainter(painter)
    {
    }

// ...

};

好吧,老实说,我并不指望第一次就把它做好,但我相信我能从错误信息中得到一些意义:

代码语言:javascript
运行
复制
1>d:\code\c++\projects\active\elesword\src\Model/Model.hpp(28): error C2063: 'std::make_unique' : not a function
1>          ..\..\src\Main.cpp(151) : see reference to class template instantiation 'Model<AssimpLoader,AssimpPainter,AssimpMesh>' being compiled

显然,MSVC并不认为std::make_unique 函数是.一个函数。

最糟糕的是,我很累,我有一种感觉,我错过了一些非常(.)很明显。有人能帮我摆脱困境吗?

另外,有人能在2015中尝试吗?只是出于好奇。

注意:,我知道我可以(也可能应该)使用return std::unique_ptr<Model>(new Model(filepath, loader, painter));,但感觉不太对。

EN

Stack Overflow用户

回答已采纳

发布于 2015-11-25 18:17:39

尝试将std函数交到一起会使您处于危险的境地,因为您正在对它们的实现进行假设,而这些实现不是标准所保证的。例如,您希望std::make_unique成为朋友,这样它就可以访问受保护的构造函数,但是如果std::make_unique的实现将此委托给其他秘密函数,怎么办?那么,你需要的是成为这个秘密函数的朋友,但它是秘密的,所以你不能。

其他复杂问题:有些形式的std::make_unique没有被标准精确地指定(虽然我认为这不适用于这个确切的例子)。在编译器完全支持变量模板之前,VC++的旧版本使用宏魔术来模拟各种模板,所以虽然有一个std::make_unqiue,但它可能没有您期望的实际签名。

票数 8
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33905782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档