lundi 28 novembre 2016

Compilation failure when deducing the return type of an auto function in an unevaluated context

I have some machinery get_return_type that can be used in an unevaluated context to deduce the return type of a function without needing to supply the argument types:

template <typename Result, typename... Args>
Result get_return_type(Result (*)(Args...));

int example(double, char, bool);

// will be int
using result_type = decltype(get_return_type(example));

However if the function is a template, and its return type is auto, all version of GCC I've tried fail to compile. Clang works perfectly.

namespace TemplatedFunctionReturningAutoTest
{
    template <typename T>
    auto foo(T v) { return v; }

    // GCC complains that Result type cannot be deduced in
    // template substitution of get_return_type
    static_assert(std::is_same<int, decltype(get_return_type(foo<int>))>::value, "");
}

Here it is on godbolt (gcc 6.2): http://ift.tt/2fFGd4A

If you switch to clang 3.x it works fine! It feels like GCC is reluctant to instantiate the body of the function to figure out its return type.

Is this a GCC bug, or is there something in the standard that prevents this from working?

Aucun commentaire:

Enregistrer un commentaire