How to Declare a Templated Struct or Class as a Friend in C ?
Nov 09, 2024 am 08:49 AMDeclaring a Templated Struct or Class as a Friend
In C programming, it is possible to declare a templated struct or class as a friend to another class, effectively allowing access to private members. However, some compilers, such as VC8, may encounter errors when implementing this feature.
Incorrect Syntax:
The following code attempts to declare a templated struct foo as a friend to all other instantiations of foo:
template <typename T> struct foo { template <typename S> friend struct foo<S>; private: // ... };
However, this will result in the error:
error C3857: 'foo<T>': multiple template parameter lists are not allowed
Correct Syntax:
To correctly declare a templated struct or class as a friend, use the following syntax:
template <typename> friend class foo;
This will allow all template instantiations of foo to be friends of each other, as desired. Therefore, the correct code should be:
template <typename T> struct foo { template <typename> friend class foo; private: // ... };
The above is the detailed content of How to Declare a Templated Struct or Class as a Friend in C ?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the types of values returned by c language functions? What determines the return value?

What are the definitions and calling rules of c language functions and what are the

C language function format letter case conversion steps

Where is the return value of the c language function stored in memory?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

How does the C Standard Template Library (STL) work?
