MSVC 2013'type':不是'std :: enable_if< false,void>的成员 [英] MSVC 2013 'type' : is not a member of 'std::enable_if<false,void>

查看:88
本文介绍了MSVC 2013'type':不是'std :: enable_if< false,void>的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 std :: enable_if 的SFINAE代码在GCC中编译.Clang,但在MSVC 2013中没有.

My SFINAE code using std::enable_if compiles in GCC & Clang, but not in MSVC 2013.

代码(也可在cpp.sh上获得)

#include <iostream>
#include <type_traits>

template <typename T, typename ... AdditionalInputs>
typename std::enable_if<sizeof...(AdditionalInputs) == 0, void>::type
CallDoDataProcessing(T var) {
    std::cout << sizeof...(AdditionalInputs) << " additional inputs" << std::endl;
}

template <typename T, typename ... AdditionalInputs>
typename std::enable_if<sizeof...(AdditionalInputs) == 1, void>::type
CallDoDataProcessing(T var) {
    std::cout << sizeof...(AdditionalInputs) << " additional inputs" << std::endl;
}

int main() {
    CallDoDataProcessing<int>(3);
    CallDoDataProcessing<int, int>(3);
    return 0;
}

在GCC/Clang中,它可以完美运行,尽管在MSVC中,我得到:

In GCC/Clang, this works perfectly, in MSVC though, I get:

Error   1   error C2039: 'type' : is not a member of 'std::enable_if<false,void>'   c:\Users\mrussell\documents\visual studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 5   1   ConsoleApplication1

已编译并运行的输出应为:

The compiled and run output should be:

0 additional inputs
1 additional inputs

我在SO上看到了一些类似的问题,但是没有一个明确的答案或稍微切线.

I've seen some similar issues on SO, but none had a clear answer or were slightly tangental.

阅读 MSVC enable_if 页,这应该可以工作...

Reading the MSVC enable_if page, this should work...

如何在MSVC2013中使用SFINAE?

How can I use SFINAE in MSVC2013?

更新

请注意,这在肯定的情况下确实有效.例如,如果我注释掉第一个函数以及对其的调用,则其余函数将被编译.即 CallDoDataProcessing 上的 enable_if< true,void> 确实具有 type 成员.

Just as a note, this does work in the positive case. For instance, if I comment out the first function, and the call to it, then the rest compiles. i.e. the enable_if<true, void> on CallDoDataProcessing does have a type member.

但是,注释掉第二个函数并对其进行调用(因此,留下 sizeof ...(AdditionalInputs)== 0 的版本不起作用.相同的错误.

However, commenting out the second function and call to it (so, leaving the version where sizeof...(AdditionalInputs) == 0 does not work though. Same error.

这表明 sizeof ...(AdditionalInputs)== 0 调用未匹配,但我不知道为什么不会如此.

This suggests that the sizeof...(AdditionalInputs) == 0 call is not being matched, but I can't figure out why it wouldn't be.

推荐答案

尝试标签分发.

template<std::size_t>
struct size {};

namespace details {
  template <typename T, typename ... AdditionalInputs>
  void CallDoDataProcessing(T var, size<0>) {
    std::cout << sizeof...(AdditionalInputs) << ", aka 0, additional inputs" << std::endl;
  }

  template <typename T, typename ... AdditionalInputs, std::size_t N>
  void CallDoDataProcessing(T var, size<N>) {
    std::cout << sizeof...(AdditionalInputs) << " additional inputs" << std::endl;
  }
}
template <typename T, typename ... AdditionalInputs>
void CallDoDataProcessing(T var) {
  details::CallDoDataProcessing<T, AdditionalInputs>( var, size<sizeof...(AdditionalInputs)>{} );
}

SFINAE确实受到MSVC的严重支持.您的代码看起来像有效的SFINAE.MSVC无法正确执行操作这一事实不足为奇.

SFINAE is really badly supported by MSVC. Your code looks like valid SFINAE. The fact that MSVC fails to do the right thing is not surprising.

MSVC在使用标记分派方面的工作效果要好得多,而且我发现它甚至可以更容易地理解代码,甚至有时还可以显示错误消息.

MSVC works much much better with tag dispatching in my experience, and I find it even results in easier to understand code and even error messages sometimes.

不允许的是在调用函数的主体之前注意不,您不能这样做",以使调用函数也声明不,我不能完成".

What it does not permit is noticing "no, you cannot do this" before the body of the calling function, to make the calling function also state "no, I cannot be done".

这篇关于MSVC 2013'type':不是'std :: enable_if&lt; false,void&gt;的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆