C++ Can';在Visual Studio 10中编译SFINAE #包括 #包括 #包括 #包括 #包括 #包括 结构X{}; 结构Y{}; __int8f(X){返回0;} __int16 f(…){返回0;} 模板typename std::enable_if::type调用(T const&T){ std::cout

C++ Can';在Visual Studio 10中编译SFINAE #包括 #包括 #包括 #包括 #包括 #包括 结构X{}; 结构Y{}; __int8f(X){返回0;} __int16 f(…){返回0;} 模板typename std::enable_if::type调用(T const&T){ std::cout,c++,c++11,visual-studio-2010,C++,C++11,Visual Studio 2010,这在VS2010中编译并正常工作。使用David的建议进行修改 #include <iostream> #include <vector> #include <algorithm> #include <utility> #include <functional> #include <type_traits> struct X {}; struct Y {}; __int8 f(X x) { return 0; } __i

这在VS2010中编译并正常工作。使用David的建议进行修改

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
#include <type_traits>

struct X {};
struct Y {};

__int8 f(X x) { return 0; }
__int16 f(...) { return 0; }

template<typename T> struct call_helper {
    static const int size = sizeof(f(T()));
};

template <typename T> typename std::enable_if<call_helper<T>::size == sizeof(__int8), int>::type call(T const& t) {
    std::cout << "In call with f available";
    f(t);
    return 0;
}

template <typename T> typename std::enable_if<call_helper<T>::size == sizeof(__int16), int>::type call(T const& t) {
    std::cout << "In call without f available";
    return 0;
}

int main() {
    Y y; X x;
    call(y);
    call(x);
}
#包括
#包括
#包括
#包括
#包括
#包括
结构X{
typedef X是_X;
};
结构Y{};
__int8f(X){返回0;}
__int16 f(…){返回0;}
模板
结构测试{
枚举{result=sizeof(f(T())};
};
模板
typename std::enable_if::类型调用(T const&T){
std::cout::type调用(T const&T){

std::cout@DeadMG:错误是什么。调用(y)或调用(x)是在模板定义(哪一个)中的吗?Comeau报告
错误:没有重载函数“call”的实例匹配参数列表。VisualC++不喜欢第一个重载的定义。有趣。@ DimMG:尝试第三函数yIn IN16调用(int i){返回0;}您可能知道它可能是变量的参数调用(…)该错误没有指定哪个调用抛出错误。这不是变量参数的问题,部分原因是我以前使用过它,部分原因是我将其替换为模板accept all,这没有任何区别。我观看了此视频(),最后说了(如果我没记错的话),编译器仅在类型完全匹配时才稳定(在eneable_if部分上).不幸的是,这段代码否定了
sizeof
hack的全部目的,它毕竟是在没有
decltype
的情况下用来比较这些类型的。我没有VS2010,但是把
test
重写成类似这样的东西怎么样:
模板结构测试{static const int value=sizeof(f(t());}然后在
enable_if
中使用它作为:
typename enable_if
@David Rodríguez-dribeas-yes-Yep,它似乎也能工作,并且不需要decltype。但我仍然无法解释为什么强制编译器通过另一种类型来获得结果。
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
#include <type_traits>

struct X {};
struct Y {};

__int8 f(X x) { return 0; }
__int16 f(...) { return 0; }

template<typename T> struct call_helper {
    static const int size = sizeof(f(T()));
};

template <typename T> typename std::enable_if<call_helper<T>::size == sizeof(__int8), int>::type call(T const& t) {
    std::cout << "In call with f available";
    f(t);
    return 0;
}

template <typename T> typename std::enable_if<call_helper<T>::size == sizeof(__int16), int>::type call(T const& t) {
    std::cout << "In call without f available";
    return 0;
}

int main() {
    Y y; X x;
    call(y);
    call(x);
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
#include <type_traits>

struct X {
  typedef X is_x;
  };
struct Y {};

__int8 f(X x) { return 0; }
__int16 f(...) { return 0; }

template < class T >
struct test {
  enum { result = sizeof(f(T())) };
  };

template <typename T>
typename std::enable_if< test<T>::result == sizeof(__int8), int>::type call(T const& t) {
    std::cout << "In call with f available" << std::endl;
    f(t);
    return 0;
}

template < typename T >
typename std::enable_if< test<T>::result == sizeof(__int16), int>::type call(T const& t) {
    std::cout << "In call without f available" << std::endl;
    return 0;
}

int main() {

    Y y; X x;
    call(y);
    call(x);
}