1

请看一下这个小示例代码

#include <type_traits>

struct base {
    template <typename T>
    int func(T);
};

struct derived: base {
    using base::func;

    template <typename T, std::enable_if_t<std::is_same_v<T,const char*>>* = nullptr>
    int func(T);
};    

auto x = derived().func(1);

它用 gcc 和 icc 编译得很好,它不能用 clang 编译,它抱怨如下:

<source>:17:20: error: no matching member function for call to 'func'

auto x = derived().func(1);
         ~~~~~~~~~~^~~~

<source>:13:9: note: candidate template ignored: requirement 'std::is_same_v<int, const char *>' was not satisfied [with T = int]

    int func(T);
        ^

好像没有using-declaration添加到struct derived. 如果我不使用模板,我肯定知道这将是 clang 的问题。但是,我不确定我正在使用模板这一事实,而 SFINAE 意味着必须使用不同的规则,所以这是我的问题:哪个编译器是正确的?Clang,还是 gcc 和 icc?

这是关于 Godbolt 问题的一个工作示例:https ://godbolt.org/z/xv98SP

4

0 回答 0