C++17,为什么自动非类型模板参数不适用于SFINAE



我正在尝试解决这个问题。

在我下面的代码中,我想使用FunctionInfo来检测由参数列表选择的函数的哪个重载。

:

decltype(MethodInfo<Foo, int>::get(&Foo::foo))

我可以根据参数列表选择正确的重载函数。

我想更进一步,能够使用:

FunctionInfo<Foo, std::tuple<int>, &Foo::foo>::type

但是当我尝试使用下面的解决方案时,HEAD 4.0.0中的clang报告如下:

error: non-type template parameter 'f' with type 'auto' has incompatible
   initializer of type '<overloaded function type>'
FunctionInfo<Foo, std::tuple<int>, &Foo::foo>::type
                                   ^~~~~~~~~

我的问题是为什么SFINAE不涉及选择哪个函数是合适的。

#include <type_traits>
#include <tuple>
template<typename T, typename... Args>
struct MethodInfo {
    template<typename Ret>
    static auto get(Ret(T::*)(Args...)) -> Ret(T::*)(Args...);
};
template<typename T, typename Tuple>
struct MethodInfoFromTuple;
template<typename T, typename...Args>
struct MethodInfoFromTuple<T, std::tuple<Args...>>{
    using type = MethodInfo<T, Args...>;
};
template<typename T, typename ArgPack, auto f,
             typename Func = decltype(MethodInfoFromTuple<T, ArgPack>::type::get(f))>
struct FunctionInfo {
    using type = Func;
};
struct Foo {
    int foo(int);
  // Uncomment this line then only first static_assert work
  //  int foo(int, int);
};
int main() {  
    static_assert(std::is_same<
                        int(Foo::*)(int), 
                        decltype(MethodInfo<Foo, int>::get(&Foo::foo))
                          >::value, "");
    static_assert(std::is_same<
                        int (Foo::*)(int), 
                        FunctionInfo<Foo, std::tuple<int>, &Foo::foo>::type
                          >::value, "");
}

重载集在c++中不是值。模板非类型参数是值。

使用特定的规则,将重载集解析为一组狭窄情况下的值,这些规则不是"尝试每种可能性,如果存在多个合法可能性,则生成错误"。相反,在某些情况下,会选择完全匹配,而在其他情况下,会对可行的重载进行排序,如果没有平局,则选择"最佳"。

传递给auto的情况都不是

最新更新