这会编译吗?

时间:2018-03-12 18:48:18

标签: c++ gcc clang compiler-bug

我有以下代码:

#include "type_traits"

template<typename T_>
struct thing{
    template<typename me_type>
    struct inner { 
        static T_& impl_0 (void* me ) { return static_cast<me_type*>(me )->operator*(); } 

        static auto getThing()
        {
            return std::integral_constant<T_& (*)(void*),&impl_0>();
        }
    };
};

它在一个模板化的类中声明了一个静态函数,该类位于另一个模板化的类中。

然后它声明一个auto函数,它返回一个指向该函数的指针,但首先它将它放入std::integral_constant

当我使用编译器标志-std=c++14向gcc版本7.3(godbolt)提出此问题时,它会抱怨:

<source>: In static member function 'static auto thing<T_>::inner<me_type>::getThing()':
<source>:11:69: error: template argument 2 is invalid
                 return std::integral_constant<T_& (*)(void*),&impl_0>();

但是当我把它交给clang 6.0.0版时,编译得很好(godbolt)。

它是否应该编译?

1 个答案:

答案 0 :(得分:1)

我认为它应该按原样编译,因为函数名称应该是可见的。但是,这解决了它:

    struct inner { 
        static auto getThing()
        {
            return std::integral_constant<T_& (*)(void*),&inner::impl_0>();
        }
    };