template friend functions of template class

后端 未结 4 1430
庸人自扰
庸人自扰 2021-01-13 07:12

I have the following template class and template function which intends to access the class\' private data member:

#include 

template

        
4条回答
  •  执笔经年
    2021-01-13 07:37

    This one compiles on MSVC2013. Basicly adds the forward declarations to class and functions before the friend

    template   class MyVar ; // class forward declaration
    
    template ; // function forward declarations
    void printVar(const MyVar& var);
    template
    void scanVar(MyVar& var);
    
    template
    class MyVar
    {
        friend void printVar(const MyVar&);
        friend void scanVar(MyVar&);
        int x;
    };
    
    template
    void printVar(const MyVar& var)
    {
        std::cout << var.x << std::endl;
    }
    
    template
    void scanVar(MyVar& var)
    {
        std::cin >> var.x;
    }
    
    struct Foo {};
    
    int main1(void)
    {
        MyVar a;
        scanVar(a);
        printVar(a);
        return 0;
    }
    

提交回复
热议问题