#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> typename std::enable_if<sizeof(f(T())) == 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<sizeof(f(T())) == 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);
}
をSFINAEをコンパイルすることはできません、それはenable_if<false, int>::type
をインスタンス化することができないということである、ここでSFINAEの非常に簡単に使用しているようだが、コンパイラはエラーをスローします。助言がありますか?どうやら、このコードはGCCでうまくコンパイルされています(どのバージョンになっているのか尋ねませんでした)。は、Visual Studioで10
編集:このコードは、だから私はbugarooneyさまで、このいずれかをチョークして完全に満足している微
#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);
}
をコンパイルします。
@DeadMG:errrorとは何ですか? call(y)かcall(x)かテンプレート定義(どちらか)のどちらかであるかどうかを確認してください。 –
Comeauは 'error:オーバーロードされた関数のインスタンスがありません。 Visual C++では、最初のオーバーロードの定義に似ていません。面白い。 –
@DeadMG:3番目の関数を試してください。__int16 call(int i){return 0; }それからそれは多分可変引数呼び出し(...)だとわかるでしょう –