Deduction guide works in gcc and msvc but rejected by clang

84 Views Asked by At

I am learning about deduction guides in C++. Then I wrote the following code that compiles with gcc and msvc but rejected by clang. Demo

I want to know which compiler is right here.

#include <vector>
#include <array>
#include <memory>

struct Coord
{    
    int x, y;
};

template<int N, int D = 0 >
class CoordArray
{ 
public:
    CoordArray(const Coord(&ref)[N]) 
    {}
};
template<int D, int N> CoordArray() -> CoordArray<N, D>;
CoordArray<3> myCoordArray{{{2, 1}, {4, 1}, {6, 1}}}; //clang rejects but gcc and msvc accepts
CoordArray myCoordArray2{{{2, 1}, {4, 1}, {6, 1}}};   //clang rejects but gcc and msvc accepts

Clang says:

<source>:17:24: error: deduction guide template contains template parameters that cannot be deduced
   17 | template<int D, int N> CoordArray() -> CoordArray<N, D>;
      |                        ^
<source>:17:14: note: non-deducible template parameter 'D'
   17 | template<int D, int N> CoordArray() -> CoordArray<N, D>;
      |              ^
<source>:17:21: note: non-deducible template parameter 'N'
   17 | template<int D, int N> CoordArray() -> CoordArray<N, D>;
      |
2

There are 2 best solutions below

0
user12002570 On BEST ANSWER

The program is ill-formed no diagnostic required since you're not using the ill-formed deduction guide. So technically, the compilers are not required to give a diagnostic.

From temp#res.general:

The program is ill-formed, no diagnostic required, if:

  • a hypothetical instantiation of a templated entity immediately following its definition would be ill-formed due to a construct (other than a static_assert-declaration that fails) that does not depend on a template parameter, or
0
Ted Lyngmo On

deduction-guide :
explicit-specifieropt template-name ( parameter-declaration-clause ) -> simple-template-id ;

From the latest C++23 draft:

13.2. Template parameters

    1. [...] A template parameter of a deduction guide template (13.7.2.3) that does not have a default argument shall be deducible from the parameter-type-list of the deduction guide template.

13.7.2.3 Deduction guides

    1. The same restrictions apply to the parameter-declaration-clause of a deduction guide as in a function declaration (9.3.4.6).

9.3.4.6 Functions

  • (1.1) — the parameter-type-list is derived from the parameter-declaration-clause

In your case, the template parameters of the deduction guide template are not deducible from the parameter-type-list, which they shall be for the deduction guide to be valid.

4.1.1 General (2.3) — Otherwise, if a program contains a violation of any diagnosable rule [...], a conforming implementation shall issue at least one diagnostic message.

[Note 1 : During template argument deduction and substitution, certain constructs that in other contexts require a diagnostic are treated differently; see 13.10.3. — end note]

13.10.3 doesn't mention deduction guides and the ill-formed deduction guide is clearly diagnosable. Clang is therefore correct in issuing a diagnostic message.