| |

VerySource

 Forgot password?
 Register
Search
View: 705|Reply: 3

Still a template issue! !! !

[Copy link]

3

Threads

10

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-1-22 10:00:02
| Show all posts |Read mode
c ++ primer 4th p553

template <class T> class A;

template <class T> class B
{
   friend class A <T>;
   friend class E <T>;
};

If the compiler is not told that the friend is a template, the compiler considers the friend to be a normal non-template class or non-template function.

What I want to ask is, how does the compiler treat E as a normal non-template class?
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-2-1 11:00:01
| Show all posts
If you write:
template <class T> class B
{
friend class A;
};

Then the compiler thinks that there is a common class called A. If you do not define such a class, then you should encounter a compilation error.
You defined A <T> and it didn't work either
Reply

Use magic Report

0

Threads

24

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-1 23:00:02
| Show all posts
What you write here is friend class E <T> ;, this means that E must be a template class, and the friend relationship between E and A is bound, that is, E and A corresponding to each T It is a friend relationship, and E and A corresponding to different T are not friend relationships.

PS: Friends and templates are troublesome. There are some syntaxes that are interpreted differently between compilers, and they seem to be a lot worse, so don't take it too seriously, otherwise you will be horny.
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-2-16 10:30:01
| Show all posts
Searchable "Befriending Templates" by herb sutter
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list