嗨,我已經衍生從C++安全布爾成語類我的類從這個頁面:The Safe Bool Idiom by Bjorn KarlssonC++安全布爾成語不能用Visual C++ 10(2010)編譯
class Element : public safe_bool<>
{
public:
bool Exists() const;
// boolean_test() is a safe_bool method
bool boolean_test() const { return Exists(); };
};
當我試圖用它if表達式像下面
Element ele;
...
if(ele)
我得到了一個錯誤C2451:類型元素「的條件表達式是非法。如果我嘗試將它轉換爲bool像下面,我得到這個錯誤
Element ele;
...
if((bool)ele)
錯誤C2440:「類型轉換」:無法從「元素」到「布爾」轉換
這是第一次時間我正在使用安全的布爾成語,我不確定這是不是允許的或Visual C++ 10中的錯誤。任何評論?提前致謝!
你有沒有試着用'類元素:公共safe_bool'?我知道這不完全是一回事,但是它與CRTP一起工作嗎? –
2010-12-14 12:54:25
「class Element:public safe_bool」不起作用。什麼是CRTP? –
ShawnWong
2010-12-15 00:36:21