Private template templates / visibility structure

I don’t understand why in the following code I am allowed to create a 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 expected behavior? error / compiler extension?

To be clear, my goal is to make a compiler error both in use print_private_templateand print_private_class.

+3
source share
2 answers

Comeau gives an error (when you comment out a function print_private_classand its call in strict C ++ 03 mode.

ComeauTest.c(31): : "A:: B" ( 7)   void print_private_template (const A:: B & ab)                                        ^            "print_private_template"    < 42U > 45

g++ 4.5 Windows -std=c++ -Wall -pedantic.

A::C A::B<T> , . , print_private_class, print_private_template .

11.8 [class.access.nest]

1 , . ; (. 11) .

+2

, GCC /, () /.

- :

template<int I> class MyTemplate
{
    struct PT
    {
        template<int, typename = void> struct InnerTemplate;
        // ... specialisations here ...
    };
public:
    typedef typename PT::template InnerTemplate<I>::SomeType SomeType;
};
typedef MyTemplate<1>::PT::InnerTemplate<1> ThisWontWork;

:

error: 'struct MyTemplate<1>::PT' is private within this context

, , PT::template, , , , , .

0

Source: https://habr.com/ru/post/1766331/


All Articles