Search code examples
c++c++11variadic-templatesusing-statement

using declaration of a specialized variadic template class


Is it possible to define FloatType in such a way that I can declare a f1 as

FloatType f1;

instead of

FloatType<> f1;

If i try to use the former i get a

error: use of class template 'FloatType' requires template arguments

template <typename T, typename... Args>
class Type
{
};

template <typename... Args>
class Type<float, Args...>
{
};

template <typename... Args>
using FloatType = Type<float, Args...>;

int
main(int, char **)
{
    FloatType<> f1;
    FloatType<float> f2;

    return 0;
}

Solution

  • No, that is impossible. From the standard §14.3/4, emphasis mine:

    When template argument packs or default template-arguments are used, a template-argument list can be empty. In that case the empty <> brackets shall still be used as the template-argument-list. [ Example:

      template <class T = char> class String;
      String<>* p; // OK: String<char>
      String* q;   // syntax error
    
      template <class ... Elements> class Tuple;
      Tuple<>* t; // OK: Elements is empty
      Tuple* u;   // syntax error
    

    —end example ]

    But then, what's wrong with writing FloatType<>? If seeing the empty brackes really irks you, you can introduce another alias for them, but that kind of obfuscates things:

    using DefFloatType = FloatType<>;
    

    Plus, it's more typing!