C++ Instantiation Problem

Yongwei Wu wuyongwei@gmail.com
Tue Jun 2 14:46:00 GMT 2015


I encountered this C++ instantiation problem. (Please guide me, if
this is not the correct list.)

The minimal test case I have is as follows:

#include <iostream>

template <typename Fn>
auto compose(Fn fn)
{
    return [fn](auto&&... x) -> decltype(auto)
    {
        return fn(std::forward<decltype(x)>(x)...);
    };
}

template <typename T1, typename T2>
auto sum(T1 x, T2 y)
{
    return x + y;
}

int main()
{
    //(void)sum<int, int>;  // GCC requires this instantiation
    auto sum_composed = compose(sum<int, int>);
    std::cout << sum_composed(1, 2) << std::endl;
}

The code cannot compile (using -std=c++14, under GCC 4.9 and 5.1),
unless I uncomment the first line in main(). The reported error is:

test.cpp: In function 'int main()':
test.cpp:21:46: error: no matching function for call to
'compose(<unresolved overloaded function type>)'
     auto sum_composed = compose(sum<int, int>);
                                              ^
test.cpp:4:6: note: candidate: template<class Fn> auto compose(Fn)
 auto compose(Fn fn)
      ^
test.cpp:4:6: note:   template argument deduction/substitution failed:
test.cpp:21:46: note:   couldn't deduce template parameter 'Fn'
     auto sum_composed = compose(sum<int, int>);
                                              ^

Clang does not complain at the code, and I cannot see why the
instantiation has to be done manually.

Is it a GCC bug? Or is the code not conformant to the C++14 standard in any way?

Thanks and best regards,

Yongwei

-- 
Wu Yongwei
URL: http://wyw.dcweb.cn/



More information about the Gcc mailing list