4

Why does the following code produce error: no viable constructor or deduction guide for deduction of template arguments of 'R'?

I've tried compiling this code on gcc.godbolt.org using all versions of Clang starting from 6.0.0 and up. They all produce the same error message. However, the code compiles with GCC 8.1 and later. Could this be a bug in Clang? Or maybe in GCC?

Compiler options used: -std=c++17 -O2 -Wall -Wextra

#include <functional>

template <typename T>
int fn(T&)
{
    return 0;
}

struct S {};

template <typename T>
struct R
{
    using F = std::function<int(T&)>;

    R(T& _t, F _fn)
        : t_{_t}
        , fn_{_fn}
    {
        fn_(t_);
    }

    T& t_;
    F fn_;
};

int main()
{
    S s;

    // Why does Clang fail on 'fn<S>'?
    // If I wrap 'fn<S>' in curly braces (e.g. {fn<S>}), it works.
    R r{s, fn<S>};

    return 0;
}
L. F.
  • 19,445
  • 8
  • 48
  • 82
Kory
  • 133
  • 1
  • 5
  • The solution mentioned in that post fixes the issue for Clang, but why does the posted code work with GCC? – Kory Nov 02 '19 at 00:23
  • Sounds like a GCC bug. Making it a non-deduced context makes the code compare in both compilers anyway :) – L. F. Nov 02 '19 at 00:31
  • Thanks! I think the other post answers my question. I'll see about reporting this to the GCC team. – Kory Nov 02 '19 at 00:43

0 Answers0