Implicit Parameter Templates, Forward Declaration, C ++

There is a template class declaration with implicit parameters:

list.h

template <typename Item, const bool attribute = true> class List: public OList <item, attribute> { public: List() : OList<Item, attribute> () {} .... }; 

I tried using the fllowing forward declaration in another header file:

Analysis.h

 template <typename T, const bool attribute = true> class List; 

But g ++ shows this error:

 List.h:28: error: redefinition of default argument for `bool attribute' Analysis.h:43: error: original definition appeared here 

If I use forward declaration without implicit parameters

 template <typename T, const bool attribute> class List; 

the compiler does not accept this construct

Analysis.h

 void function (List <Object> *list) { } 

and shows the following error (i.e. does not take an implicit value):

 Analysis.h:55: error: wrong number of template arguments (1, should be 2) Analysis.h:44: error: provided for `template<class T, bool destructable> struct List' Analysis.h:55: error: ISO C++ forbids declaration of `list' with no type 

Updated question:

I removed the default option from the template definition:

list.h

 template <typename Item, const bool attribute> class List: public OList <item, attribute> { public: List() : OList<Item, attribute> () {} .... }; 

The first file, using the List class, has a forward declaration with an implicit parameter attribute value

Analysis1.h

 template <typename T, const bool attribute = true> class List; //OK class Analysis1 { void function(List <Object> *list); //OK }; 

Second class using List WITH forward definition using implicit value

Analysis2.h

 template <typename T, const bool attribute = true> // Redefinition of default argument for `bool attribute' class List; class Analysis2 { void function(List <Object> *list); //OK }; 

Second class using List WITHOUT forward definition class using implicit value

Analysis2.h

 template <typename T, const bool attribute> // OK class List; class Analysis2 { void function(List <Object> *list); //Wrong number of template arguments (1, should be 2) }; 
+7
source share
5 answers

Simple Remove the default value from the definition, as you already mentioned this in the ad forward.

 template <typename Item, const bool attribute = true> //<--- remove this 'true` class List: public OList <item, attribute> { //.. }; 

Record:

 template <typename Item, const bool attribute> //<--- this is correct! class List: public OList <item, attribute> { //.. }; 

Online Demo: http://www.ideone.com/oj0jK

+5
source

A possible solution is to declare another List_fwd.h header file.

 template <typename Item, const bool attribute> class List; 

So in List.h and Analysis.h you include List_fwd.h at the beginning. Thus List.h becomes

 #include "List_fwd.h" template <typename Item, const bool attribute = true> class List: public OList <item, attribute> { public: List() : OList<Item, attribute> () {} ... }; 

And Analysis.h

 #include "List_fwd.h" 
+2
source

you can define the default parameter in only one place (for a given translation). this is most useful for this in a class declaration, and it is a bad idea to define it when going forward.

the striker does not need a default parameter (you just need to enter it in some cases).

you can create another simple type of template that implements this in combination with forwarding if you really want to use the default parameter. then you get the result using typedef. You can do this by forwarding the list.

0
source

You must ensure that only the first declaration has a default value for the parameter. This can be done by first defining the title for the direct declaration only, then including it from both List.h and Analysis.h . In the definition in List.h do not include the default value.

0
source

You must include List.h in every file where you use it. Declarations are sufficient only for non-standard types. For template types, you must include a header file for each compilation unit.

0
source

All Articles