1

我目前正在从 gcc 4.9 (c++1) 升级到 gcc 8.2 (c++14),我在 gcc 8.2 上遇到问题,代码如下

#include <type_traits>
#include <iostream>

template <class T>
struct has_foo_method 
{
   struct not_found {};

   template <class C>
   static auto f(C* e) -> decltype(foo(*e));

   template <class>
   static auto f(...) -> not_found;

   using type = decltype(f<T>(nullptr));
   static const bool value = !std::is_same<not_found, type>::value;
};

template <class E, class E00 = std::decay_t<E>>
std::enable_if_t<!has_foo_method<E00>::value, typename has_foo_method<E00>::type>
foo(E&& e)
{
    return foo(std::forward<E>(e));
}

struct toto
{};

int main()
{
    std::cout << "same type : "<< (std::is_same<toto, std::decay<toto>::type>::value ? "TRUE" : "FALSE") << std::endl;
    std::cout << "foo methode  : " << (has_foo_method<toto>::value ? "TRUE" : "FALSE") << std::endl;
    std::cout << "foo methode for decay: " << (has_foo_method<std::decay_t<toto>>::value ? "TRUE" : "FALSE") << std::endl;
    foo(toto{});
    return 0;
}

错误如下:

source>: In substitution of 'template<class E, class E00> std::enable_if_t<(! has_foo_method<E00>::value), typename has_foo_method<E00>::type> foo(E&&) [with E = toto&; E00 = <missing>]':
...
<source>:19:20: fatal error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum)

你可以在这里找到代码和错误

我还发现,如果我删除 E00 = std::decay_t 并像这样替换每个 E 的 E00 :

template <class E>
std::enable_if_t<!has_foo_method<E>::value, typename has_foo_method<E>::type>

代码正在编译,但我不明白为什么。

你知道为什么它不在 gcc 8.2 上编译而是在 gcc 4.9 上吗?

4

0 回答 0