相关疑难解决方法(0)

具有自动返回类型扣除的朋友功能模板无法访问私有成员

很抱歉这个问题的标题有多复杂; 我试图描述我为这个问题构建的最小SSCCE.

我有以下代码:

#include <iostream>

namespace fizz
{
    template<typename... Ts>
    class bar
    {
    public:
        template<int I, typename... Us>
        friend auto foo(const bar<Us...> &);

    private:
        int i = 123;
    };

    template<int I, typename... Ts>
    auto foo(const bar<Ts...> & b)
    {
        return b.i;
    }
}

int main()
{
    std::cout << fizz::foo<1>(fizz::bar<int, float>{});
}
Run Code Online (Sandbox Code Playgroud)

此代码使用GCC 5.2进行编译,不是使用Clang 3.7进行编译:

main.cpp:19:18: error: 'i' is a private member of 'fizz::bar<int, float>'
        return b.i;
                 ^
main.cpp:25:24: note: in instantiation of function template specialization …
Run Code Online (Sandbox Code Playgroud)

c++ templates friend language-lawyer c++14

25
推荐指数
1
解决办法
559
查看次数

标签 统计

c++ ×1

c++14 ×1

friend ×1

language-lawyer ×1

templates ×1