VS2013上的SFINAE错误?

vmrob

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

有任何想法吗?有没有更好的选择?这里的想法是,如果Function接受NumArgs表示的数字或参数,我想对Function进行函数调用。最后两个可变参数应转发给函数,并返回结果。

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)...);
    }
};
爵士先生

我通过将代码更改为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 ++还不喜欢在模板参数列表中转换0void*,因此我将其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>’

事实证明,问题是我将模板声明intsize参数的,但是编译器得到的std::size_t与并不相同int,即使您可以轻松地在它们之间进行转换。
在上面的示例中,我什至无法替换= 0= NULL因为那只是一个0L文字,我将不得不= (void*)0让编译器接受它(因为默认类型enable_if<true>::typevoid)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

VS2013上的模板专业化和SFINAE

来自分类Dev

VS2013或VS2015显示“未指定的错误”

来自分类Dev

VS2013上Windows上的全局包含路径

来自分类Dev

VS2013:查找资源字典时发生错误

来自分类Dev

vs2013的cordova混合APP构建错误

来自分类Dev

VS2013可变参数模板编译错误

来自分类Dev

WPF VS2013:查找资源字典时发生错误

来自分类Dev

如何修复vs2013的C3848错误?

来自分类Dev

内联成员缺少返回的错误(VS2013)

来自分类Dev

剃刀视图中的Mvc5 VS2013错误

来自分类Dev

VS2013 devenv命令行生成错误/挂起

来自分类Dev

未解决的外部符号错误(VS2013 Express)

来自分类Dev

剃刀视图中的Mvc5 VS2013错误

来自分类Dev

为什么VS2013中的#warning会产生错误?

来自分类Dev

尽管使用了Microsoft.Net.Compilers nuget包,VS2013上仍存在C#6错误消息

来自分类Dev

VS2013的命令提示符预编译的Orchard 1.8.x版本在.ts文件上提供错误

来自分类Dev

DrawContours上的OpenCV 2.4.8和VS2013崩溃

来自分类Dev

缺少VS2013上的新xaml控件

来自分类Dev

打字稿VS2013

来自分类Dev

VS2013的奇怪行为

来自分类Dev

SFINAE 在 VS2017 上的编译错误

来自分类Dev

LINQ设计时间错误从VS2010升级到VS2013

来自分类Dev

Clang + VS2013:包含C ++标头时发生错误(vs2012:working)

来自分类Dev

LINQ设计时间错误从VS2010升级到VS2013

来自分类Dev

错误SIPEPS,版本= 5.0.0.0 UCMA 4.0 VS2010 / VS2013

来自分类Dev

当我在PC上安装vs2013时安装vs2015

来自分类Dev

外部VS2013构建错误“错误MSB4019:找不到导入的项目<路径>”

来自分类Dev

为什么C ++编译器(VS2013)选择错误的函数?

来自分类Dev

VS2013:错误加载解决方案('JavascriptWebExtensionsPackage无法正确加载')

Related 相关文章

热门标签

归档