如何成为模板类函数的朋友? [英] How to befriend a templated class function?

查看:65
本文介绍了如何成为模板类函数的朋友?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保持func变量私有,所以我试图与它成为朋友,下面的代码不能在VS2017中编译,错误C2248:'wrapper :: _ Instance :: func':无法访问私有在'wrapper :: _ Instance'类中声明的成员。

I want to keep func variable private, so I'm trying to befriend it and the code below doesn't compile in VS2017 with an error C2248: 'wrapper::_Instance::func': cannot access private member declared in class 'wrapper::_Instance'.

如何使其有效?

#include "pch.h"
#include <iostream>

class wrapper
{
private:
    // ...

public:


    template <class T>
    void FetchStructList(T);

    // instance
    class _Instance {

        int (*func)(const char*, int*, double*);

    public:


        _Instance() {

            // func = &externalFunction;

        };

        template <class T>
        // friend void wrapper::FetchStructList<T>(T); doesn't compile either
        friend void wrapper::FetchStructList(T);

    } Instance;

    void FetchInstanceExtentions() { FetchStructList(Instance); };
};

template <class T>
void wrapper::FetchStructList(T s)
{
    int size = 0;
    int res = 0;

    if constexpr (std::is_same<T, _Instance>::value)
        res = s.func(nullptr, &size, nullptr);
        // ...
    else {
        // ...
        return;
    }

    //...
}

int main()
{
    wrapper w;

    w.FetchInstanceExtentions();
}

推荐答案

在Visual Studio窗口的右上角有一个发送反馈按钮。

In the top right of the Visual Studio window there is a send feedback button.

单击此按钮并选择报告问题。

Click this and select Report a Problem.

在Visual中Studio Feedback窗口,确保您无法找到具有相同内容的反馈。如果您不能将此报告为错误。确保您提供代码作为问题的示例,并确保您提到这应编译
并且GCC / Clang接受此作为有效代码。

In the Visual Studio Feedback window that make sure that you can't find feedback with the same content. If you can't then report this as a bug. Make sure you provide the code as an example of the problem and make sure that you mention that this should compile and GCC/Clang accept this as valid code.


这篇关于如何成为模板类函数的朋友?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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