【问题标题】:Should this compile?这应该编译吗?
【发布时间】:2018-08-20 21:37:21
【问题描述】:

我有以下代码:

#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>();
        }
    };
};

它在另一个模板类中的模板类中声明一个静态函数。

然后它声明一个自动函数,它返回一个指向该函数的指针,但首先它把它放入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)。

是否应该编译?

【问题讨论】:

  • 如果我用-std=c++17把它交给gcc,它会编译。它也可以用 gcc trunk 编译

标签: c++ gcc clang compiler-bug


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2012-02-13
    • 2011-07-11
    • 1970-01-01
    • 2013-06-20
    • 2014-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2013-11-22
    相关资源
    最近更新 更多