OpenMeshエラー「Usedereference operators->and*instead.」の解決

3028 ワード

今日直接先生からもらった資料の中の数年前のコードをコンパイルして、OpenMesh 7.1のライブラリを使って、handle()関数の中で“This function clutters your code.Use dereference operators->and*instead.”に出会った.の問題で、検索ではネット上の解答は多くなく、OpenMeshのチュートリアルの多くは数年前のもので、比較的時代遅れであることが分かった.ヒントに従ってコードを変更して解決したが,これにより多くの関連問題が引き出され,以前の関数がOpenMesh 7でサポートされなくなった原因が多い.ここでは、現在deprecatedされている2つの関数をまとめます.
1. handle()
サンプルコード:
        //       
	for (auto it = mesh.vertices_begin(); it != mesh.vertices_end(); it++) 
	{
		auto point = mesh.point(it.handle());//     ,    auto point = mesh.point(*it);

		std::cout << point[0] << "," << point[1] << "," << point[2] << std::endl;
	}

ここでitは反復器ですがmesh.ポイント(it.handle()ここでは「This function clutters your code.Usedereference operators->and*instead.」と間違って報告されます.handle()ではなく*を使うようにします.つまりit.handle()を*itに変更すればいいのですが、この2つのコード機能はまったく同じです.
エラーの原因:handleの定義では、handle()がhnd_を返すことがわかります.そしてこのhnd_の定義はprotected:value_handle hnd_.value_handleタイプは実際には反復器のvalue_ですtype、typedefに来ました.したがってhandle()は反復器が指す値を返します.現在handle機能は廃棄されており、公式ドキュメントでも説明されています.あなたが使う限り、コンパイラは間違っているので、*を変更すればいいです.
        /**
         * \brief Get the handle of the item the iterator refers to.
         * \deprecated 
         * This function clutters your code. Use dereferencing operators -> and * instead.
         */
        DEPRECATED("This function clutters your code. Use dereferencing operators -> and * instead.")
        value_handle handle() const {
            return hnd_;
        }

*の定義では、*演算子の機能がreturn hnd_であることがわかります.元のhandle()に代わって
        /// Standard dereferencing operator.
        reference operator*() const {
            return hnd_;
        }

        /// Standard pointer operator.
        pointer operator->() const {
            return &hnd_;
        }

2.反復器から値タイプへの暗黙的な変換
サンプルコード:
        //        
	for (auto it = mesh.halfedges_begin(); it != mesh.halfedges_end(); ++it)
	{
            //        ,  it    ,       handle  。   it  *it
	    std::cout << mesh.from_vertex_handle(it) << " ";
	    std::cout << mesh.to_vertex_handle(it) << std::endl;
	}

古いコードでは、OpenMeshでこのような暗黙的な変換が定義されているため、このように書くことができます.
        /**
         * \brief Cast to the handle of the item the iterator refers to.
         * \deprecated
         * Implicit casts of iterators are unsafe. Use dereferencing operators
         * -> and * instead.
         */
        DEPRECATED("Implicit casts of iterators are unsafe. Use dereferencing operators -> and * instead.")
        operator value_handle() const {
            return hnd_;
        }

しかし、これは安全ではないと考えられています.反復器と反復器のhnd_が混同されないように、したがって、暗黙的な変換はサポートされません.暗黙的な変換を使用すると、「Implicit casts of iterators are unsafe.Usedereference operators->and*instead.」というメッセージが表示されます.
 
実際、どうしても使うなら、ライブラリのヘッダファイル定義に対応するDERPRECATED()の行を削除すれば、エラーは報告されません.