Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8665107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:24:12+00:00 2026-06-12T17:24:12+00:00

I have a question for Specific Template Friendship in C++. in the book C++

  • 0

I have a question for Specific Template Friendship in C++.
in the book C++ Primer, the specific template friendship is written like this:

 template <class T> class Foo3;
 template <class T> void templ_fcn3(const T&);
 template <class Type> class Bar {
     // each instantiation of Bar grants access to the
     // version of Foo3 or templ_fcn3 instantiated with the same type
     friend class Foo3<Type>;
     friend void templ_fcn3<Type>(const Type&);
     // ...
 };

The special point is that there is

<Type>

after the class or function name in the friend statement.

However,in practice, if I write this:

template <class Type> class T_CheckPointer;
template <class T> T_CheckPointer<T> operator+(const T_CheckPointer<T> &, const size_t n);

template <typename Type>
class T_CheckPointer {

    // Specific Template Friendship
    friend T_CheckPointer<Type>
    operator+ <Type> (const T_CheckPointer<Type> &, const size_t n);

// other code...

}

There will be a error during instantiations for the template function.

And if I change

// Specific Template Friendship
friend T_CheckPointer<Type>
    operator+ <Type> (const T_CheckPointer<Type> &, const size_t n);

to

// Specific Template Friendship
friend T_CheckPointer<Type>
    operator+ <> (const T_CheckPointer<Type> &, const size_t n);

by deleting the word type after the function name, then all will be okay.

Anyone can tell me the reason?


For information, there is the error message when I call

int iarr[] = {1, 2, 3, 4};
T_CheckPointer<int> itcp(iarr, iarr+4);

error message:

/usr/include/c++/4.4/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<int>’:
/usr/include/c++/4.4/bits/stl_iterator.h:96:   instantiated from ‘std::reverse_iterator<int>’
../Classes/T_CheckPointer.hpp:31:   instantiated from ‘T_CheckPointer<int>’
../PE16.cpp:520:   instantiated from here
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:127: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:128: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:129: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:130: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:131: error: ‘int’ is not a class, struct, or union type
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:24:13+00:00Added an answer on June 12, 2026 at 5:24 pm

    Here’s a minimal example:

    template<typename T> struct U { typedef typename T::X X; };
    template<typename T> void foo(typename U<T>::X);
    
    template<typename T> struct S;
    template<typename T> void foo(S<T>);
    template<typename T> struct S { friend void foo<T>(S<T>); };
    
    template struct S<int>;
    

    The reason the friend declaration fails is that by providing a full list of template arguments you are requesting the compiler specialize all available function templates and pick the one that best matches the signature. Specialization of the first definition of foo results in specializing U with an argument that results in an ill-formed program.

    If instead you omit the template argument, it will be deduced from the arguments. As such template argument deduction is performed according to 14.8.2 [temp.deduct], and in particular 14.8.2p8 applies, which means that the substitution failure in the specialization of U is not an error (SFINAE).

    This is a good reason to omit template arguments anywhere they can be deduced from context (e.g. here the function or operator parameter types). Note that you still need to provide the <> brackets to ensure that the operator + is read as a template-id (14.5.4 [temp.friend]).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this specific question to do to you, i have a database from
This is quite a specific question, and I have had no luck on the
( Disclaimer: This question is not specific to ASP.NET) I have a control which
So I have the following method: template <class DT> //needs testing DT& LinkedSortedArrays<DT>::find (const
I have a specific question about looping variable names in WPF. XAML code looks
I have a specific question, that could use a general answer... When building Multi-Tier
I have a fairly specific question about concurrent programming in C. I have done
I have a question about whether or not a specific way of applying of
I have a quick question about encapsulating specific types with typedef . Say I
Sorry for a pretty specific question. I have a table (see bottom), and when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.