Clang does not notice default template parameters

  • A+
Category:Languages

Background

According to the C++ standard, when forward-declaring a template type with default template parameters, each of them can appear in one declaration only. For example:

// GOOD example template <class T = void> class Example;    // forward-declaration  template <class T> class Example {}; // definition 
// GOOD example template <class T> class Example;    // forward-declaration  template <class T = void> class Example {}; // definition 
// BAD example template <class T = void> class Example;    // forward-declaration  template <class T = void> // ERROR: template parameter redefines default argument class Example {}; // definition 

Problem

In my code I have a lot of forward declaration in different files, so it makes sense to put my default parameters in the definition:

// foo.hpp, bar.hpp, baz.hpp, etc. template <class T> class Example; 
// example.hpp template <class T = void> class Example {}; 

and, as expected, it all works well everywhere... except clang! I narrowed the problem down to this:
In clang, if the class template has default parameters, but they are not declared in the first forward declaration of that class, and when declaring an instance of that class no angle brackets are specified, clang ignores the default parameter and raises an error "no viable constructor or deduction guide for deduction of template arguments of ...".

Example

// GOOD example template <class T> class Example;  template <class T = void> class Example {};  int main() {     Example e; // error: no viable constructor or deduction guide for deduction of template arguments of 'Example' } 
  • Moving = void to the forward declaration fixes the issue, but is not viable for me since my forward-decls are in different files and I don't know which one will appear first. (Also super problematic since my defaults would be in some obscure file deep in the codebase)
  • Changing Example e; to Example<> e; fixes the issue, but is not viable for me since I'm a library dev and don't want all my users typing <> after my classes.
  • Adding a forward-declaration file example_fwd.hpp with one forward-declaration and including it instead of forward declaring every time fixes the issue, but I would like to avoid this if there is a better solution.

Question

Who is right in this case: clang or the other compilers? Is this a compiler bug? How can I circumvent this issue (apart from partial solutions I described above)? I've found #10147 (and related stackoverflow questions), but it's about template template params and also is marked as fixed over a year ago.

 


Considering the following:

[temp.param]/12 - The set of default template-arguments available for use is obtained by merging the default arguments from all prior declarations of the template in the same way default function arguments are [ Example:

template<class T1, class T2 = int> class A; template<class T1 = int, class T2> class A; 

is equivalent to

template<class T1 = int, class T2 = int> class A; 

— end example ]

The default arguments available for

template <class T> class Example;  template <class T = void> class Example {}; 

will be the defaults arguments in the definition of Example. The two declarations above will be equivalent to have a single declaration as

template <class T = void> class Example {}; 

which will effectively allow doing Example e.

The original code should be accepted. As a workaround and already suggested in max66's answer, you can provide a deduction guide that uses the default argument

Example() -> Example<>; 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: