c++三目演算子のピット

411 ワード

7,8年のc++のコードを書いて、初めて三木演算子でピットに遭遇しました.
pstrValue = ProbeBind(pstrValue) ? ParseBind(pstrValue) : pstrValue;

ParseBindの戻り値のタイプはpstrValueとは異なるタイプであり、その結果、演算時にProbeBind関数がtrueまたはfalseを返してもpstrValueがParseBindの戻り値タイプにプライバシー変換され、一時的なオブジェクトがバグを生成することが分かった.
この方法で次の問題を解決します.
pstrValue = ProbeBind(pstrValue) ? decltype(pstrValue)(ParseBind(pstrValue)) : pstrValue;