Call operator with auto return type being chosen instead of constructor when using std::function

Viewed 819

The following snippet:

#include <functional>

struct X {
    X(std::function<double(double)> fn); // (1)
    X(double, double);                   // (2)

    template <class T>
    auto operator()(T const& t) const {  // (3)
        return t.foo();
    }
};

int main() {
    double a, b;
    auto x = X(a, b);
    return 0;
}

...fails to compile with both clang (4.0.1) and g++ (6.3, 7.2) when using -std=c++14 — tested on OSX and godbolt.org.

But it compiles without problem if:

  • I remove constructor (1);
  • or I set the return type to a concrete type (e.g. double) in (3);
  • or if I use a trailing return type (-> decltype(t.foo())) in (3);
  • compile using -std=c++1z (thanks @bolov).

Maybe there is something obvious that I am missing here... Is there any issue with this code? Is this a bug?

1 Answers
Related