jeudi 29 mars 2018

Using a template class with defaults for all the template arguments [duplicate]

I have a template class with default template arguments provided for all the template arguments. If I try to use the template class without providing any template arguments, I expect to use the template instantiation using all those defaults, just like for function template arguments. But instead the compiler complains about the template arguments missing. Does C++ permit all the template class arguments to have default values?

Here is a minimal example that demonstrates the problem:

 #include <chrono>

 template<
   typename T = std::chrono::duration< int >,
   typename D = std::chrono::duration< unsigned int >
   >
 class Range final
 {
  public:

   using Time = TIME;

   Range(const T t_):
     t(t_)
     {
     }

  private:
   T t;
 };


 class Container final
 {
  public:

   Range getRange() const
   {
     return Range{Range::Time{0}};
   }

 };

My compiler complains as follows:

 main.cpp:27:3: error: invalid use of template-name ‘Range’ without an argument list
    Range getRange() const
    ^~~~~
 main.cpp:7:13: note: ‘template<class T, class D> class Range’ declared here
  class Range final
              ^~~~~

Aucun commentaire:

Enregistrer un commentaire