Search code examples
c++templateslanguage-lawyerc++17template-argument-deduction

Deduction guides, templates and subobjects: which compiler is right?


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?


Solution

  • 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.