16

Consider the following snippet:

struct S {
    S() {}

    template<typename B>
    struct T {
        T(B &&) {}
    };

    template<typename B>
    T(B &&) -> T<B>;
};

int main() {
    S::T t{0};
}

Clang accepts it while GCC rejects the code with the following error:

prog.cc:10:5: error: deduction guide 'S::T(B&&) -> S::T' must be declared at namespace scope

Is this valid code? Which compiler is right, GCC or Clang?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
skypjack
  • 49,335
  • 19
  • 95
  • 187
  • A little [bit more complex example](https://stackoverflow.com/questions/46103102/how-to-provide-deduction-guide-for-nested-template-class) seem to still fail in both gcc and clang so don't be overoptimistic with clang ability to compile nested deduction-guides :) – W.F. May 30 '18 at 11:19
  • @W.F. Apparently CTAD doesn't work for classes inside template classes; standard defect (see my comment on the linked answer). – Rakete1111 May 31 '18 at 09:35
  • now fixed in GCC 12 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79501) – Desmond Gold Apr 12 '22 at 01:00

1 Answers1

15

According to http://en.cppreference.com/w/cpp/language/class_template_argument_deduction

User-defined deduction guides must name a class template and must be introduced within the same semantic scope of the class template (which could be namespace or enclosing class) and, for a member class template, must have the same access, but deduction guides do not become members of that scope.

So clang seems correct.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    I saw it, but I failed to find something along this line in the standard. Can you help on this? – skypjack May 30 '18 at 09:11
  • @skypjack from [\[temp.deduct.guide\]](https://eel.is/c++draft/temp.deduct.guide#3) : "A deduction-guide shall inhabit the scope to which the corresponding class template belongs and, for a member class template, have the same access." – El Mismo Sol Mar 03 '22 at 13:50