私は、struct InferValueType
参照型を含む他の型に暗黙的に変換できる を持っています。次のコードを使用してこれを実現します。
struct InferValueType {
// ...
template <typename Type>
operator Type() const && {
// ...
}
// We need this special version to allow conversions to reference types
template <typename Type>
operator Type&() const & {
// ...
}
};
InferValueType
ユーザーによってインスタンス化されることはありません。代わりに、関数の戻り値として提供します。
const InferValueType read();
との組み合わせによりInferValueType
、read
ユーザーは次のような遅延コードを記述できます。
int i = read();
MyType t1 = read();
MyType& t2 = read();
const MyType& t3 = read();
これを GCC 4.8、4.9、5、6、および Clang 3.7 と 3.8 に対してテストしました。GCC 4.8 だけがあいまいさについて文句を言います。
conversion from ‘const InferValueType’ to ‘int’ is ambiguous
conversion from ‘const InferValueType’ to ‘MyType’ is ambiguous
これは、 and をそれぞれ and を使用して構築できるためだと思いint
ます。MyType
const int&
const MyType&
質問:これは 4.9.2 より前の GCC の特性ですか、それとも実際には C++11 でのあいまいさですか?
完全な例はこちらです。