私はoperator[]
のための簡単なチェックを書きましたが、has_subscript_op
構造体テンプレートのインスタンスが間違ったオーバーロードを選択します:SFINAEの演算子[]のチェックが私よりも混乱していますか?
#include <iostream>
#include <type_traits>
#include <string>
#include <map>
template<class, class, class = void>
struct has_subscript_op : std::false_type
{ };
template<class T, class S>
struct has_subscript_op<T, S, std::void_t<decltype(&std::declval<T>()[S()])>> : std::true_type
{ };
int main()
{
//true, nice
std::cout << "int[][int]: " << has_subscript_op<int[], int>::value << std::endl;
//false, nice
std::cout << "int[][float]: " << has_subscript_op<int[], float>::value << std::endl;
//true, nice
std::cout << "std::string[int]: " << has_subscript_op<std::string, int>::value << std::endl;
//true, WAT?
std::cout << "std::map<std::string, std::string>[int]: " << has_subscript_op<std::map<std::string, std::string>, int>::value << std::endl;
}
私はGCC 6.2.0に
を使用していますが、このGCCのバグ、一般的なバグです、または私はどこかに明白な間違いをした?
私はgccを使いましたが、コンパイラで5〜6個のバグが見つかりました。与えられたコンパイルの失敗がコンパイラのバグの結果である可能性は非常に小さいです。 –
'std :: map s; auto b = s [0]; ' - これはうまくコンパイルされ、あなたが見ている結果を説明します。さて、なぜ[0] 'が動作するのか、これは別の質問でしょう。 –
@BaummitAugen最後の1つです。例のマップは 'operator [](int)'ではなく、 'operator [](std :: string)'を持っていなければなりません。 – xinaiz