3
をテンプレートクラスのメンバ関数を選択するようにenable_if_tを使用して、私はそうのような一体型クラスのテンプレートパラメータに基づいて、異なるメンバ関数を有効にしようとしています:C++ - 14整数型に
C++でgcc5を使用して#include <type_traits>
template<int Dimension>
struct Foo
{
template<std::enable_if_t<Dimension == 1> = 0>
int bar(int i) const { return i; }
template<std::enable_if_t<Dimension == 2> = 0>
int bar(int i, int j) const { return i + j; }
};
int main(int argc, const char **argv)
{
Foo<1> a;
a.bar(1);
Foo<2> b;
b.bar(1,2);
return 0;
}
から14モード、それは以下のエラーでコンパイルに失敗します。
tools/t1.cpp: In instantiation of 'struct Foo<1>':
tools/t1.cpp:18:12: required from here
tools/t1.cpp:13:9: error: no type named 'type' in 'struct std::enable_if<false, int>'
int bar(int i, int j) const { return i + j; }
^
tools/t1.cpp: In instantiation of 'struct Foo<2>':
tools/t1.cpp:21:12: required from here
tools/t1.cpp:10:9: error: no type named 'type' in 'struct std::enable_if<false, int>'
int bar(int i) const { return i; }
これらはSFINAEがenable_if_tが正常に機能しているように見えるので、私は期待して何をしていません示されているように見えます。
この簡単な例では、オーバーロードも同様に機能しますが、私の実際の使用例では、環境によって誤った使用やコンパイルエラーを防ぐために関数を隠す必要があります。
SFINAEには何がありますか?
パーフェクト。ありがとうございました。 – RandomBits
@ T.C。私は強く、あなたの文章、*置換の失敗はゾウではないと考えています*、(実際には偽であるとは言えませんが)エラーが含まれています。 – skypjack
ヒント*ガード・パック*、そのテクニックは考えられませんでした:* + 1 *。 – skypjack