C++で使い捨て変数を作成することはできますか?ここでC++には移動と削除のセマンティクスがありますか?
は私が達成したいものの一例である:ここで
const float _phiTemp = atan2(tan(cluster.beta), tan(cluster.alpha));
const float phi = HALF_PI - std::abs(HALF_PI - std::abs(_phiTemp));
// After running this code I want _phiTemp to be unaccessible, and the
// compiler to send an error if I ever try
は、私が何をしたいの長いと醜い実装です:
const float phi = 0;
{
const float _phiTemp = atan2(tan(cluster.beta), tan(cluster.alpha));
float& phiRef = const_cast<float&> phi;
phiRef = HALF_PI - std::abs(HALF_PI - std::abs(std::move(_phiTemp)));
}
// _phiTemp is disposed and phi is a const, and safely used in the calculation
// through std::move()
私は何かが足りないのですか? C++に「即時」の変数処分はありませんか?
"* C++に"即時 "の変数処分はありませんか?*"あります。既にその例があります。スコープを示すには角かっこを使用し、スコープは寿命を示します。それについて醜いものはありません。 –
まあ、瞬時には、 "現在のスコープでスタックから変数を取り除く"という意味です(+コンパイラが変数がなくなったことを知るようにします)。 –
'phi'からconstをキャストし、それを使って代入すると未定義の振る舞いがあります。 – molbdnilo