4

Visual Studio 2010SP1で使用しています。次のコードは、コンパイラをクラッシュさせます。

template <typename T>
class MyClass
{
public:
  typedef int my_int;

  const my_int foo();

};

template <typename T>
const auto MyClass<T>::foo() -> my_int
// auto MyClass<T>::foo() -> const my_int // THIS WORKS!
{
  return my_int(1);
}

int main()
{
  MyClass<int> m;
  m.foo();
}

問題を修正するコメント行に注意してください。autoここで適切に使用していますか (つまりconst、修飾子 on auto)? 回避策は本質的にまったく同じですか (つまり、コンパイラのバグが修正されるまで安全に使用できますか)? 最後に、この問題が発生しているのは私だけですか? そうでない場合は、バグ レポートを提出します。

注:ここでの const はほとんど意味がないことを理解しています。const実際のプロジェクトではオブジェクトへの参照を返す小さなプロジェクトでバグを再現しようとしていました。

4

2 に答える 2

4

The code is ill-formed in C++11: if there is a trailing return type, then the "normal" return type must be auto (the C++11 specification states at 8.3.5[dcl.fct]/2 that "T shall be the single type-specifier auto," where T is the "type" that appears before the name of the function).

All compiler crashes are compiler bugs, so it is a bug that the Visual C++ 2010 compiler crashes when compiling your program. This bug has been fixed, though; Visual C++ 2013 rejects the program with a proper compilation error.

于 2014-01-02T02:52:32.577 に答える
1

const autoこれは、複数のコンパイラでコードを試してみると、末尾の戻り値の型を使用するとエラーになることに気付くのに役立つ場合の 1 つです。いくつかのオンライン C++ コンパイラがあります。このコードを で試した場合clang、次のエラーが表示されます (実際の例):

エラー: 末尾の戻り値の型を持つ関数は、'const auto' ではなく、戻り値の型 'auto' を指定する必要があります

ドラフト C++ 標準の関連するセクションは、8.3.5 関数の段落2のセクションであり、次のように述べられています (強調鉱山):

宣言 TD で、D の形式は次のとおりです。

D1 ( parameter-declaration-clause ) cv-qualifier-seqopt
  ref-qualifieropt exception-specificationopt attribute-specifier-seqopt
    trailing-return-type

[...] T は単一の型指定子 auto .[...]

于 2014-01-02T03:18:32.733 に答える