In C++, can I declare a template struct with a template parameter unused in the definition? -


recently i'm reading source code spirit-v2-json, , have been confused following code:

template <typename tag> struct literal  {      bool operator==(literal const&) const { return true;  }      bool operator< (literal const&) const { return false; }  };  typedef literal<struct tag_undefined> undefined; typedef literal<struct tag_null>  null; 

question 1: what's meaning of tag parameter in template of literal? unused. question 2: struct tag_undefined , struct tag_null haven't been defined declared, why can use them template parameters?

the purpose of tag parameter differentiate between different types. there no requirement them display difference in functionality, need distinct classes. differentiation purpose of tag parameter , isn't used in way in template class itself, ok use incomplete in-situ types directly in typedef.

why want this? in above use case code uses template define 2 distinct types, undefined , null. have created types directly, using struct null , struct undefined, have had define operators operator= , operator< separately, considered waste. using type tag in way achieves same effect template code re-use.

if later on in code 2 types passed parameters boost::variant, "multi-type, single value" methodology represent values in variable type. here code wants have concept of undefined , null distinct types in multi-type system (i guess represent undefined or null json objects).

update: per op request, here code demonstrate incomplete types can , can not used:

struct incomplete_struct; struct complete_struct{};  template<typename tag> struct some_templ {};  template<typename tag> struct some_other_templ {     tag member; };  int main() {     some_templ<incomplete_struct> a;          //ok - incomplete template param type not used in class not instantiated     some_templ<struct inline_incomplete> b;   //ok - above      some_other_templ<complete_struct> c;      //ok  - complete_struct complete type     some_other_templ<incomplete_struct> d;    //compile error - incomplete type, , used in template - instantiation of incomplete_struct required impossible      return 0; }  

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -