私有模板类/结构体可见性 [英] Private template classes/structs visibility

查看:146
本文介绍了私有模板类/结构体可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么在下面的代码中,我允许创建函数 print_private_template ,同时编译器抱怨 print_private_class

  #include< cstdio> 

class A
{
private:
template< unsigned T>
struct B
{

};

struct C
{

};

public:
template< unsigned T>
B< T> getAb()
{
return B< T>();
}

C getAc()
{
return C();
}
};

template< unsigned T>
void print_private_template(const A :: B< T& ab)
{
printf(%d\\\
,T);
}

void print_private_class(const A :: C& ac)
{
printf(something\\\
);
}

int main(int,char **)
{
A a;

print_private_template(a.getAb< 42>());

print_private_class(a.getAc());

return 0;
}

这是一个预期的行为吗?编译器错误/扩展?



为了清楚起见,我的目标是使编译器错误 c> print_private_template 和 print_private_class

解决方案

Comeau 发生错误(当您注释掉 print_private_class


ComeauTest.c(31):error:class template基于$ b $在实例化print_private_template期间检测到的A :: B(在第7行声明)是不可访问的
void print_private_template(const A :: B& ab)
^ b在第45行的模板参数< 42U>


Windows上的G ++ 4.5不报告 c

和类模板 A :: B< T> 都具有与任何其他正常成员相同的可见性。因此, print_private_class print_private_template 需要诊断。



< >

11.8嵌套类 [class.access.nest]



1 类是成员,因此具有与任何其他成员相同的访问权限。
的一个封闭类的成员对嵌套类的成员没有特殊访问权限;应遵守通常的访问规则(第11条)



I don't understand why in the following code, I am allowed to create the function print_private_template while the compiler complains about print_private_class:

#include <cstdio>

class A
{
    private:
        template <unsigned T>
        struct B
        {

        };

        struct C
        {

        };

    public:
        template <unsigned T>
        B<T> getAb()
        { 
            return B<T>();
        }

        C getAc()
        { 
            return C();
        }
};

template<unsigned T>
void print_private_template(const A::B<T> &ab)
{
    printf("%d\n", T);
}

void print_private_class(const A::C &ac)
{
    printf("something\n");
}

int main(int, char**)
{
    A a;

    print_private_template(a.getAb<42>());

    print_private_class(a.getAc());

    return 0;
}

Is this an expected behaviour? a compiler bug/extension?

Just to be clear, my goal is to make the compiler error on both the usage of print_private_template and print_private_class.

解决方案

Comeau does give an error (when you comment out the print_private_class function and its call in strict C++03 mode.

ComeauTest.c(31): error: class template "A::B" (declared at line 7) is inaccessible void print_private_template(const A::B &ab) ^ detected during instantiation of "print_private_template" based on template argument <42U> at line 45

G++ 4.5 on Windows does not report any error with -std=c++ -Wall -pedantic though.

Your class A::C and class template A::B<T> both have the same visibility as any other normal members. Hence, both print_private_class and print_private_template require a diagnostic.

11.8 Nested classes [class.access.nest]

1 A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules (Clause 11) shall be obeyed.

这篇关于私有模板类/结构体可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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