Search code examples
c++templatescoding-styleclass-template

Avoid angle brackets in default template


If I have a template class with a default template type, I have to write the template angle brackets. Is it somehow possible to avoid this?

Example:

template <typename T=int>
class tt {
public:
  T get() { return 5; }
};

...

tt<> t;  // how to avoid <>
std::cout << t.get() << std::endl;

Until now i've did this by a separate namespace and redeclaring the class:

namespace detail_ {
template <typename T=int>
class tt {
public:
  T get() { return 5; }
};
}

class tt : public detail_::tt {}

...

tt t;
std::cout << t.get() << std::endl;

The problem is, if I want to use the class with an other type I have to go over namespace detail_. Is there another solution, which I didn't see yet.


Solution

  • Since C++17, because of class template argument deduction, things have changed.

    tt and tt<> are not the same thing: types and class templates were different and continue to be treated differently.

    Anyway in simple scenarios like the one in your example, C++17 assumes what you mean and the <> aren't needed anymore.

    Further details: