gpt4 book ai didi

c++ - VS2013 上的 SFINAE 错误?

转载 作者:搜寻专家 更新时间:2023-10-31 01:47:48 25 4
gpt4 key购买 nike

我一直在尝试任何我能想到的方法来获得 _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)...);
}
};

最佳答案

我通过将您的代码更改为 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>’

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

关于c++ - VS2013 上的 SFINAE 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18745814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com