c++ - Friend template functions can't access private members -


i have question regarding this code written @gmannickg.

i going see if understood going on edited print_binary_helper's friend functions (original code has been commented):

//template <typename u> //friend print_binary_helper<u> print_binary(u value); friend print_binary_helper<t> print_binary(t value);  //template <typename u> //friend std::ostream& operator<<(std::ostream& sink, //  const print_binary_helper<u> source); friend std::ostream& operator<<(std::ostream& sink,     const print_binary_helper<t> source);  //template <typename u> //friend std::wostream& operator<<(std::wostream& sink, //  const print_binary_helper<u> source); friend std::wostream& operator<<(std::wostream& sink,     const print_binary_helper<t> source); 

to use t instead of u program won't compile. explain me did wrong , if possible , if is, how done?

i'm using vc++ 11 , error get:

1>anything.cpp(68): error c2248: 'print_binary_helper<t>::print_binary_helper' : cannot access private member declared in class 'print_binary_helper<t>' 1>          1>          [ 1>              t=int 1>          ] 1>          anything.cpp(31) : see declaration of 'print_binary_helper<t>::print_binary_helper' 1>          1>          [ 1>              t=int 1>          ] 1>          anything.cpp(73) : see reference function template instantiation 'print_binary_helper<t> print_binary<int>(t)' being compiled 1>          1>          [ 1>              t=int 1>          ] 1>anything.cpp(68): error c2248: 'print_binary_helper<t>::print_binary_helper' : cannot access private member declared in class 'print_binary_helper<t>' 1>          1>          [ 1>              t=unsigned __int64 1>          ] 1>          anything.cpp(31) : see declaration of 'print_binary_helper<t>::print_binary_helper' 1>          1>          [ 1>              t=unsigned __int64 1>          ] 1>          anything.cpp(75) : see reference function template instantiation 'print_binary_helper<t> print_binary<unsigned __int64>(t)' being compiled 1>          1>          [ 1>              t=unsigned __int64 1>          ] 

template <typename u> friend print_binary_helper<u> print_binary(u value); 

makes template print_binary function friend.

friend print_binary_helper<u> print_binary(u value); 

makes non template print_binary function friend.

the 2 different. in case template function not friend , non template function not defined. don't errros because not using non-template print_binary anywhere.

the functions friends. should not depend on template argument of class. should independent functions.


if want make t specializations of functions friends t specializations of print_binary_helper class can forward declare functions , speicalize them did in class minor modifications. thing this.

template <typename t> class print_binary_helper;  template <typename t> std::ostream& operator<<(std::ostream& sink,                          const print_binary_helper<t> source);  template <typename t> std::wostream& operator<<(std::wostream& sink,                           const print_binary_helper<t> source);  template <typename t> print_binary_helper<t> print_binary(t value);  template <typename t> class print_binary_helper { public:     static_assert(std::is_integral<t>::value,                   "cannot print non-integer in binary.");      //make  print_binary<t> friend print_binary_helper<t>     friend print_binary_helper<t> print_binary<>(const t value);                                 //            ^^          friend std::ostream& operator<< <>(std::ostream& sink,                                 //  ^^                                       const print_binary_helper<t> source);      friend std::wostream& operator<< <>(std::wostream& sink,                                 //   ^^                                      const print_binary_helper<t> source); 

here example of that.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -