473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Templated friends syntax

Banfa
9,065 Expert Mod 8TB
So I have a little problem, I have a template class and that class contains a template function; now what I want to do is declare that function in the class (or indeed the entire class) as a friend of all specialisations of the class.

I did that (or thought I did) but the code I produced compiles with gcc 3.4.2 but not with Microsoft Visual Studio 2008 (Express Edition). So I have reduced my code to a minimum compilable example that exhibits the behaviour as follows
Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. class Example
  3. {
  4. template<class TT> template<class RHS> friend void Example<TT>::copy(RHS& rhs);
  5.  
  6. public:
  7.     // Default constructor
  8.     Example() : data(0) { }
  9.  
  10.     //Destructor deletes managed object when reference count is zero
  11.     ~Example(){ delete data; }
  12.  
  13.  
  14.     template<class R>
  15.     R* asType() { return data; }
  16.  
  17.     template<class R>
  18.     const R* asType() const { return data; }
  19.  
  20.     template<class RHS>
  21.     void copy(RHS& rhs);
  22.  
  23. protected :
  24.     T* data;
  25. };
  26.  
  27. template<class T>
  28. template<class RHS>
  29. void Example<T>::copy(RHS& rhs)
  30. {
  31.     this->~Example();
  32.  
  33.     data = rhs.template asType<T>();
  34. }
  35.  
  36.  
  37. int main(int, char**)
  38. {
  39.     Example<int> ei;
  40.  
  41.     Example<int> el;
  42.  
  43.     ei.copy<Example<int> >(el);
  44. }
  45.  
One of the results of the production is that the code does appear to make logical sense, that is it appears more complex than required, however it does require that level of complexity in the full code.

So for gcc 3.4.2 using the command line

g++ -Wall -pendantic TemplateText.cpp

compiles into an executable without producing any compiler diagnostic messages.

However for Microsoft cl version 15.00.30729.01 (as shipped with Visual Studio 2008 Express Edition) the following errors are produced

Expand|Select|Wrap|Line Numbers
  1. TemplateTest.cpp(4) : warning C4346: 'Example<T>::copy' : dependent name is not a type
  2.         prefix with 'typename' to indicate a type
  3.         TemplateTest.cpp(25) : see reference to class template instantiation 'Example<T>' being compiled
  4. TemplateTest.cpp(4) : error C2998: 'Unique-Type-copy copy' : cannot be a template definition
  5. TemplateTest.cpp(8) : error C3861: 'data': identifier not found
  6. TemplateTest.cpp(8) : error C2461: 'Example<T>' : constructor syntax missing formal parameters
  7. TemplateTest.cpp(8) : error C2473: '{ctor}' : looks like a function definition, but there is no parameter list.
  8. TemplateTest.cpp(11) : error C2143: syntax error : missing ';' before '~'
  9. TemplateTest.cpp(34) : error C2039: 'copy' : is not a member of 'Example<T>'
  10. TemplateTest.cpp(4) : warning C4346: 'Example<T>::copy' : dependent name is not a type
  11.         prefix with 'typename' to indicate a type
  12.         TemplateTest.cpp(39) : see reference to class template instantiation 'Example<T>' being compiled
  13.         with
  14.         [
  15.             T=int
  16.         ]
  17. TemplateTest.cpp(4) : error C2998: 'Unique-Type-copy copy' : cannot be a template definition
  18. TemplateTest.cpp(8) : error C3861: 'data': identifier not found
  19. TemplateTest.cpp(8) : error C2461: 'Example<T>' : constructor syntax missing formal parameters
  20.         with
  21.         [
  22.             T=int
  23.         ]
  24. TemplateTest.cpp(8) : error C2473: '{ctor}' : looks like a function definition, but there is no parameter list.
  25. TemplateTest.cpp(11) : error C2143: syntax error : missing ';' before '~'
  26. TemplateTest.cpp(43) : error C2039: 'copy' : is not a member of 'Example<T>'
  27.         with
  28.         [
  29.             T=int
  30.         ]
So can anyone here verify that the code on line 4 is in fact standard conforming syntax or show that it isn't?

Alternatively can anyone think of a way of solving the problem described in the first paragraph?
Nov 18 '09 #1
5 3987
weaknessforcats
9,208 Expert Mod 8TB
It looks like you want an unbounded friend function in the Example class.

Unbounded friends require the template for the friend be embedded in the class so the compiler has access to it.

Bounded friends are specializations so in that case all you need is a function prototype showing the correct type.

This compiles using Visual Studio.NET 2008:

template<class T>
Expand|Select|Wrap|Line Numbers
  1. class Example 
  2. friend class RHS;
  3. friend void copy(RHS& rhs)
  4.     this->~Example(); 
  5.  
  6.     data = rhs.template asType<T>(); 
  7. etc....
Nov 18 '09 #2
RRick
463 Expert 256MB
In some cases, templates have trouble figuring out the type of what something is. I don't know the specifics of what causes this, but sometimes an explicit "typename" is needed in a template declaration.

Try googling for "c++ difference between typename and class". It returns some interesting answers.
Nov 18 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
The answer is:

Older C++ implementations used class in templates to identify a type:

Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. void func()
  3. {
  4.     T data;
  5. }
even though T may not be a class. Like it could be an int or a double.

Newer C++ implementations use typename instead of class to better communicate that you are dealing with a type, whether it is a class ot not.

Expand|Select|Wrap|Line Numbers
  1. template<typename T>
  2. void func()
  3. {
  4.     T data;
  5. }
Prefer typename to class in templates.
Nov 19 '09 #4
Banfa
9,065 Expert Mod 8TB
The explicit typename is need where you are using the templated type to access a type contained in the templated type. For instance

Expand|Select|Wrap|Line Numbers
  1. template<typename T>
  2. class MyTemplateClass
  3. {
  4. public:
  5.     // constructors methods etc.
  6.  
  7. protected
  8.     typename T::size_type size;
  9. }
  10.  
typename is required to tell the compiler that T::size_type refers to a type defined in T. In my case the compiler has got confused and suggested I have left out a required typename, however I copy is a function not a type so this doesn't apply.
Nov 19 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
That's true. It used to be you had to code:

Expand|Select|Wrap|Line Numbers
  1. template<class T> 
  2. class MyTemplateClass 
  3. public: 
  4.     // constructors methods etc. 
  5.  
  6. protected:
  7.     class T::size_type size; 
  8. };
So in those cases where T was not a class, your code looked odd. So they changed it to typename.
Nov 20 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Pete C. | last post by:
I'm trying to make a templated function a friend like this: class cls { ... friend cls func<> (const cls& a, const cls& b); }; template< template<class> class Op> cls func (const cls& a,...
2
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions...
6
by: __PPS__ | last post by:
it's ok according to the standard to specialize member function like this: template<class T>struct xxx { void func(); }; template<>bool xxx<int>::func(){ //int version } template<>bool...
6
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
2
by: | last post by:
Hello All, I am having a lot of difficulty trying to bind a templated column, that is programmatically created for a datagrid, to a datasource column. I have a datasource containing 2 columns,...
9
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a...
15
by: kwikius | last post by:
Hi all, I have been searching through the list of papers at: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/ I seem to remember a proposal regarding templated namespaces e.g: template...
2
by: thekestrel | last post by:
Hi, I'm trying to iterate over the values of a list that has a templated type and I'm having trouble with the syntax, could anyone offer somehelp. Simplified version... #include <list>...
3
by: Rickarazzi | last post by:
This is a problem that arose while using GNU G++ 3.4.5 under Linux. The problem is: How to get a pointer value from a templated object inside a class? Normally, I would add an '&' can carry on. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.