VS2013上的SFINAE错误?

时间:2013-09-11 15:49:36

标签: c++ templates c++11 sfinae visual-studio-2013

我一直在尝试任何我能想到的事情让_CallWithRightmostArgsInner功能正常失败,以便SFINAE可以正常工作并且通过这次尝试,VS2013给了我错误: error C2039: 'type' : is not a member of 'std::enable_if<false,void>'

有什么想法吗?有没有更好的选择?这里的想法是,我想对Function进行函数调用,前提是Function接受NumArgs表示的一个或多个参数。应该将最后两个可变参数转发给函数并返回结果。

template <typename Function, int NumArgs>
class SplitParameters {
public:
    typedef typename function_traits<Function>::result_type result_type;

    template <typename ... RightArgs>
    static result_type CallWithRightmostArgs(const Function& call, RightArgs && ... rightArgs) {
        static_assert(sizeof...(RightArgs) >= NumArgs, "Unable to make function call with fewer than minimum arguments.");
        return _CallWithRightmostArgs(call, std::forward<RightArgs>(rightArgs)...);
    }

private:
    template <typename ... RightArgs>
    static result_type _CallWithRightmostArgs(const Function& call, RightArgs && ... rightArgs) {
        return _CallWithRightmostArgsInner(call, std::forward<RightArgs>(rightArgs)...);
    }

    // note the '==' vs '!=' in these two functions.  I would assume that only one could exist
    template <typename LeftArg, typename ... RightArgs, typename std::enable_if<sizeof...(RightArgs) != NumArgs>::type* = 0>
    static result_type _CallWithRightmostArgsInner(const Function& call, LeftArg, RightArgs && ... rightArgs) {
        return _CallWithRightmostArgs(call, std::forward<RightArgs>(rightArgs)...);
    }

    template <typename LeftArg, typename ... RightArgs, typename std::enable_if<sizeof...(RightArgs) == NumArgs>::type* = 0>
    static result_type _CallWithRightmostArgsInner(const Function& call, LeftArg, RightArgs && ... rightArgs) {
        return call(std::forward<RightArgs>(rightArgs)...);
    }
};

1 个答案:

答案 0 :(得分:3)

我通过将代码更改为

来使用g ++ - 4.8
    #include <iostream>

    template <class T>
    struct function_traits
    {
        typedef void result_type;
    };

    template <typename Function, int NumArgs>
    class SplitParameters {
    public:
        typedef typename function_traits<Function>::result_type result_type;

        template <typename ... RightArgs>
        static result_type CallWithRightmostArgs(const Function& call, RightArgs && ... rightArgs) {
            static_assert(sizeof...(RightArgs) >= NumArgs, 
                          "Unable to make function call with fewer than minimum arguments.");
            return _CallWithRightmostArgs(call, std::forward<RightArgs>(rightArgs)...);
        }

    private:
        template <typename ... RightArgs>
        static result_type _CallWithRightmostArgs(const Function& call, RightArgs && ... rightArgs) {
            return _CallWithRightmostArgsInner(call, std::forward<RightArgs>(rightArgs)...);
        }

        // note the '==' vs '!=' in these two functions.  I would assume that only one could exist
        template <typename LeftArg, typename ... RightArgs, class = typename std::enable_if<sizeof...(RightArgs) != NumArgs -1 >::type>
        static result_type _CallWithRightmostArgsInner(const Function& call, LeftArg, RightArgs && ... rightArgs) {
            return _CallWithRightmostArgsInner(call, std::forward<RightArgs>(rightArgs)...);
        }

        template <typename ... RightArgs, class = typename std::enable_if<sizeof...(RightArgs) == NumArgs>::type>
        static result_type _CallWithRightmostArgsInner(const Function& call, RightArgs && ... rightArgs) {
            return call(std::forward<RightArgs>(rightArgs)...);
        }
    };

    void f(int i, int j)
    {
        std::cout << i << ' ' << j << std::endl;
    }

    int main()
    {
        SplitParameters<decltype(f), 2>::CallWithRightmostArgs(f, 1, 2, 3, 4);
    }

编译器不喜欢您从_CallWithRightmostArgs调用_CallWithRightmostArgsInner,我认为您实际上是在尝试调用Inner函数。
g ++也不喜欢在模板参数列表中将0转换为void*,所以我改为将其改为class = enable_if<...>::type

我没有详细研究它失败的原因,希望这对你来说已经足够了。

修改: 关于typename enable_if<...>::type* = 0遭到拒绝,我记得std::array存在类似问题:

    template <class T, int size>
    void f(const std::array<T,size>&){}

这个小片段可以自行编译,但是当你这样做时:

    std::array<int,4> a;
    f(a);

    g++ gives:
    test3.cpp: In function ‘int main()’:
    test3.cpp:9:8: error: no matching function for call to ‘f(std::array<int, 4ul>&)’
         f(a);
            ^
    test3.cpp:9:8: note: candidate is:
    test3.cpp:4:6: note: template<class T, int size> void f(const std::array<T, size>&)
     void f(const std::array<T,size>&){}
          ^
    test3.cpp:4:6: note:   template argument deduction/substitution failed:
    test3.cpp:9:8: note:   mismatched types ‘int’ and ‘#‘integer_cst’ not supported by dump_type#<type error>’
         f(a);
            ^
    test3.cpp:9:8: note:   ‘std::array<int, 4ul>’ is not derived from ‘const std::array<T, size>’

事实证明,问题是我将模板声明为int size参数,但编译器获得的是std::size_t不一样的作为int即使您可以轻松地在它们之间进行转换 在上面的示例中,我甚至无法用= 0替换= NULL,因为这只是0L字面值,我必须= (void*)0才能让编译器接受它(因为enable_if<true>::type的默认类型是void)。